Checking for an exception

Steve D'Aprano steve+python at pearwood.info
Sun Jun 25 13:31:06 EDT 2017


On Mon, 26 Jun 2017 02:40 am, Skip Montanaro wrote:

>> 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? 

Yes I do, which is why I was demonstrating to Paul that his suggestion to use
Exception instead of BaseException is not good enough.

*wink*

> Also, Checking isinstance() between two classes isn't 
> likely to work, I don't think.

Indeed it doesn't, which is why I was demonstrating that Paul's suggestion to
use isinstance instead of issubclass is not good enough.

*wink*


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

Indeed it is. Which is why in my original post I used BaseException.

*eye spasms from winking too much*


> 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.

No, user-defined exceptions must derive from BaseException too.




-- 
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