Exception hierarchy
We've already encountered many of the most common built-in exceptions, and you'll probably encounter the rest over the course of your regular Python development. As we noticed above, most exceptions are subclasses of the Exception
class. But this is not true of all exceptions. Exception
itself actually inherits from a class called BaseException
(In fact, all exceptions must extend the BaseException
class or one of its subclasses). There are two key exceptions, SystemExit
and KeyboardInterrupt
, that derive directly from BaseException
instead of Exception
.
SystemExit
is an exception that is raised whenever the program exits naturally, typically because we called the sys.exit
function somewhere in our code (for example, because ...
Get Python 3 Object Oriented Programming now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.