Checking for an exception

Steve D'Aprano steve+python at pearwood.info
Sun Jun 25 12:10:03 EDT 2017


On Sun, 25 Jun 2017 05:50 pm, Paul Rubin wrote:

> Steve D'Aprano <steve+python at pearwood.info> writes:
>> What's the right/best way to test whether an object is an exception
>> ahead of time? (That is, without trying to raise from it.)
> 
> Maybe I'm missing something but
>    isinstance(obj, Exception)
> seems to work.

Not well enough I'm afraid:



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





-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.




More information about the Python-list mailing list