11

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.

1
  • Did you actually check your statement "isinstance(SystemExit(1), Exception) evals to True" on Python 2.6? Commented May 22, 2014 at 7:13

3 Answers 3

13

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

Sign up to request clarification or add additional context in comments.

2 Comments

Oh, and GeneratorExit was subsequently moved in 2.6 to inherit directly from BaseException since it is a "system-exiting" exception in some styles of generator-based programming.
11

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.

Comments

8

Your error is in the very first sentence of your question:

>>> isinstance(SystemExit(1), Exception)
False

SystemExit is not a subclass of Exception.

1 Comment

Thanks, I found the real error, is in Python 2.3, isinstance(SystemExit(1), Exception) is True. And with Python 2.3, the testing code prints "caught by except Exception". For Python 2.6, this is correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.