isinstance(SystemExit(1), Exception)
evals to True, but this snippet prints "caught by bare except SystemExit(1,)"
.
try:
sys.exit(0)
except Exception, e:
print 'caught by except Exception', str(e)
except:
print 'caught by bare except', repr(sys.exc_info()[1])
My testing environment is Python 2.6.
isinstance(SystemExit(1), Exception)
is False on Python 2.6. Exception hierarchy in this version of Python was changed since Python 2.4.
E.g. KeyboardInterrupt
is not subclass of Exception
any more.
See more info http://docs.python.org/release/2.6.6/library/exceptions.html#exception-hierarchy
SystemExit
derives from BaseException directly rather than from Exception.
Exception
is the parent "All built-in, non-system-exiting exceptions"
SystemExit is a "system exiting exception" (by definition) and therefore doesn't derive from Exception
. In your example, if you used BaseException
, it would work as per your original assumptions.
Your error is in the very first sentence of your question:
>>> isinstance(SystemExit(1), Exception)
False
SystemExit
is not a subclass of Exception
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With