Exception Handling in Python
Tomek Lisowski
Lisowski.Tomasz at sssa.SPAM-OFF.com.pl
Wed Oct 18 09:13:01 EDT 2000
Użytkownik "Quinn Dunkan" <quinn at zloty.ugcs.caltech.edu> napisał w
wiadomości news:slrn8upe01.ero.quinn at zloty.ugcs.caltech.edu...
> On Tue, 17 Oct 2000 06:10:29 GMT, Suchandra Thapa
> <ssthapa at harper.uchicago.edu> wrote:
> > I'm trying to figure out how to catch an exception, do some
> >error handling and then reraising the same exception in python.
> >Right now, I'm doing it using the following code:
> >
> >try:
> > ...
> >except Foo:
> > ...
> >except Bar:
> > ...
> >except Exception, x:
> > ...
> > raise x
> >else:
> > ...
>
> Other people have pointed out the argumentless 'except' and 'raise', but
if
> what you want to do is some "cleanup", you may really want 'finally':
>
> try:
> try:
> ...
> except Foo:
> ...
> except Bar:
> ...
> else:
> ...
> finally:
> ...
IMHO the proper version is given below:
try:
try:
...
finally:
...
except Foo:
...
except Bar:
...
except:
...
because after try ... except ... the exception IS already handled, so there
is no need to surround it once more in a try ... finally ... construction,
which in your case does not even know that an exception has been raised. My
version includes try ... finally ... inside try ... except ... construction,
which makes the necessary cleanup first, then responds to the exception. The
other correction is that else: is replaced by except:, which handles all
other exception types.
Best regards
Tomasz Lisowski
More information about the Python-list
mailing list