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.
Eg 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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.