David542
David542

Reputation: 110267

Order of Exception evaluations

In python exceptions, does the TypeError check occur before doing the ValueError check? For example:

>>> chr(123)
'{'

>>> chr('x')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required (got type str)

>>> chr(18293939)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: chr() arg not in range(0x110000)

>>> chr(1829393993939393)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: signed integer is greater than maximum

Or what is the order in which the various errors are checked? Is there documentation on the order of operations/evaluations for the difference Exception types?

Upvotes: 3

Views: 58

Answers (1)

Davis Herring
Davis Herring

Reputation: 39878

None of this is documented (i.e., guaranteed). That said, it would be impossible to raise a sensible ValueError if there was no value of the correct type to compare. The difference between ValueError and OverflowError is surely an implementation detail entirely, since anything that overflows the target type would of course be out of any restricted range for that type.

Upvotes: 2

Related Questions