[Tutor] Is there a convenient table of Python 3.4 exceptions?

Ben Finney ben+python at benfinney.id.au
Fri Oct 24 06:04:54 CEST 2014


boB Stepp <robertvstepp at gmail.com> writes:

> My current understanding is that exception1, ... , exceptionN should
> match one of Python's exception classes or subclasses in order to
> perform a match and execute the corresponding exception suite.

Correct. Bear in mind that “… or subclasses” covers user-defined
exception classes, so you'll often see non-standard exception classes in
use by particular code bases, that are nevertheless subclasses of the
exception classes in the standard library.

> ​I have so far been unable to find a list of these class/subclass
> names.

The standard library documentation's chapter on exceptions
<URL:https://docs.python.org/3/library/exceptions.html> shows
<URL:https://docs.python.org/3/library/exceptions.html#exception-hierarchy>.

But you won't find an exhaustive list of what can raise those
exceptions.

> Of course I can force an error to occur in the interpreter and see
> what comes up for each type of error I wish to catch.

Yes, that's the right thing to do; that, and read the documentation for
whatever library code you are invoking. It should say if there are
important situations where a particular exception will be raised.

There will, of course, be a great many other situations that can raise
an exception. This is a normal part of the flow of code in Python, and
the standard library does a lot of it.

More importantly, though, you should consider *why* you're attempting to
catch a lot of exception classes. Will you be meaningfully handling
every one of those situations? That's rather doubtful.

Instead, you should consider which exceptional states your code can
meaningfully handle, discover what excpetion classes are raised for
those few situations, and catch *only* those classes.

Any other exception that gets raised isn't something you can do anything
useful with, so it should propagate back up to higher levels, either to
be handled or to exit with a useful error message.

-- 
 \         “Science is a way of trying not to fool yourself. The first |
  `\     principle is that you must not fool yourself, and you are the |
_o__)               easiest person to fool.” —Richard P. Feynman, 1964 |
Ben Finney



More information about the Tutor mailing list