Exception Handling in Python
Alex Martelli
aleaxit at yahoo.com
Tue Oct 17 04:30:46 EDT 2000
"Suchandra Thapa" <ssthapa at harper.uchicago.edu> wrote in message
news:slrn8unl3s.3br.ssthapa at localhost.localdomain...
> 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:
> ...
>
> which seems to work but I wanted to know if there are any problems with
> this method or if there is a better way to do it. The only potential
problem
> I know of right now is that user defined exception not derived from
Exception
> will slip through but is that much of a problem?
Hard to say how bad it is. But you can work around it -- add a
clause:
except:
print "some VERY peculiar exception...!"
raise
Section 6.8 in the Language Reference says "If no expressions are present,
raise re-raises the last expression that was raised in the current scope.".
E.g.:
>>> try:
raise "feep"
except Exception, x:
print "There",x
raise x
except:
print "here"
raise
here
Traceback (innermost last):
File "<pyshell#23>", line 2, in ?
raise "feep"
feep
>>>
Alex
More information about the Python-list
mailing list