Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #15791

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

From Terry Reedy <tjreedy@udel.edu>
Subject Re: try - except. How to identify errors unknown in advance?
Date 2011-11-16 16:18 -0500
References <1321462647.2315.31.camel@hatchbox-one>
Newsgroups comp.lang.python
Message-ID <mailman.2784.1321478316.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: try - except. How to identify errors unknown in advance? Terry Reedy <tjreedy@udel.edu> - 2011-11-16 16:18 -0500

csiph-web