[Tutor] exception question
Gonçalo Rodrigues
op73418 at mail.telepac.pt
Wed Apr 28 07:31:02 EDT 2004
Em Wed, 28 Apr 2004 09:48:59 +0100, Dave S <pythontut at pusspaws.net>
atirou este peixe aos pinguins:
>Is it possible, for the code after except to know the exception type. I
>know I can do multiple "except"s for each condition but this is not
>realy what I want.
>
>try:
> ....
> ...code...
> ....
>except:
> ...code ....
>
Sure. The following is, I believe, self-explanatory:
>>> try:
... raise TypeError
... except ValueError:
... print "Got a ValueError"
... except TypeError:
... print "Got a TypeError"
...
Got a TypeError
You can also "catch" the exception object itself:
>>> try:
... raise TypeError("This is a type error")
... except TypeError, e:
... print "TypeError found!"
... print "Error: %r." % e
... print "Arguments of exception object: %r." % e.args
...
TypeError found!
Error: <exceptions.TypeError instance at 0x010DD530>.
Arguments of exception object: 'This is a type error'.
>>>
Hope it helps, with my best regards,
G. Rodrigues
More information about the Tutor
mailing list