Why "except Exception" doesn't catch SystemExit?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle4
--
Chapters
00:00 Question
00:32 Accepted answer (Score 13)
00:59 Answer 2 (Score 11)
01:31 Answer 3 (Score 8)
01:50 Thank you
--
Full question
https://stackoverflow.com/questions/5714...
Accepted answer links:
http://docs.python.org/release/2.6.6/lib...
Answer 2 links:
[BaseException directly rather than from Exception]: http://docs.python.org/library/exception...
["All built-in, non-system-exiting exceptions"]: http://docs.python.org/library/exception...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #exceptionhandling
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Puzzle4
--
Chapters
00:00 Question
00:32 Accepted answer (Score 13)
00:59 Answer 2 (Score 11)
01:31 Answer 3 (Score 8)
01:50 Thank you
--
Full question
https://stackoverflow.com/questions/5714...
Accepted answer links:
http://docs.python.org/release/2.6.6/lib...
Answer 2 links:
[BaseException directly rather than from Exception]: http://docs.python.org/library/exception...
["All built-in, non-system-exiting exceptions"]: http://docs.python.org/library/exception...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #exceptionhandling
#avk47
ACCEPTED ANSWER
Score 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
ANSWER 2
Score 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.
ANSWER 3
Score 8
Your error is in the very first sentence of your question:
>>> isinstance(SystemExit(1), Exception)
False
SystemExit is not a subclass of Exception.