Catching Exceptions.
Jeremy Jones
zanesdad at bellsouth.net
Mon Sep 27 12:06:58 EDT 2004
Keith Bolton wrote:
>I am handling exceptions currently using try, except. Generally I don't
>handle specific exceptions and am catching all.
>Then if an exception occurs, I would like to capture that error string.
> However, in the documentation it seems like
>there is not a way to get the extra str data if you are handling all
>exceptions and not specifically. Is there?
>
>thanks
>
>-keith
>
>
You want to specify the specific exception you want to handle like this:
>>> f = {'a': 'b'}
>>> f['b']
Traceback (most recent call last):
File "<stdin>", line 1, in ?
KeyError: 'b'
>>> try:
... print f['b']
... except KeyError:
... print "You got a key error"
... except:
... print "You got some other kind of exception than a KeyError"
...
You got a key error
>>>
Just name the exception after the "except". You can list a chain of
"except <some_specific_exception>" followed by a generic "except" to
handle any other type of exception you didn't mention.
Jeremy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040927/db3fc307/attachment.html>
More information about the Python-list
mailing list