try - except. How to identify errors unknown in advance?

Terry Reedy tjreedy at udel.edu
Wed Nov 16 16:18:14 EST 2011


On 11/16/2011 11:57 AM, Frederic Rentsch wrote:

> If I don't know in advance which error to expect, but on the contrary
> want to find out which error occurred, I can catch any error by omitting
> the name:
>
> 	except: (handle)
>
> But now I don't have access to the error message 'e'. I'm sure there's a
> way and it's probably ridiculously simple.

Bare except is a holdover from when exceptions could be strings rather 
than an instance of a subclass of BaseException. A Python 3 interpreter 
in effect runs code within a try-except block something like this:

try:
     <your code>
except BaseException as __exception__:
     <print traceback and exit>

However, use Exception instead of BaseException in your code unless you 
REALLY know what you are doing and why.

-- 
Terry Jan Reedy




More information about the Python-list mailing list