Suppose I have an exception class with an abstract base class, something like this:
class MyExceptions(BaseExeption, metaclass=abc.ABCMeta): pass class ProperSubclass(MyExceptions): pass MyExceptions.register(ValueError)
It looks like I can catch a ProperSubclass on MyExceptions , but not a ValueError :
try: raise ProperSubclass() except MyExceptions: print('As expected, control comes here...') except: print('...and not here.') try: raise ValueError() except MyExceptions: print('Control does not come here...') except ValueError: print('...but unexpectedly comes here.')
So my question is, should I handle the built-in exceptions with their abstract base class? If so, how? And if not, what are the rules?
I guess another way to ask the question is: do except clauses use isinstance () / issubclass () correctly for matching, and if not (as it happens), what do they use? Perhaps there are some shady shortcuts in the C implementation.
python exception abstract-base-class
abingham
source share