Checking for an exception

Skip Montanaro skip.montanaro at gmail.com
Sun Jun 25 12:40:28 EDT 2017


>
> py> isinstance(KeyboardInterrupt(), Exception)
> False
> py> isinstance(ValueError, Exception)
> False
>

I might have missed something, but don't you want to be using BaseException
as your class/type? Also, Checking isinstance() between two classes isn't
likely to work, I don't think.

Both the 2.7 and 3.6 docs indicate that BaseException is the base class for
all built-in exceptions:

https://docs.python.org/2/library/exceptions.html
https://docs.python.org/3/library/exceptions.html

Of course, YMMV when dealing with user-defined exception classes.

Skip



More information about the Python-list mailing list