Exception handling

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Wed May 8 16:44:21 EDT 2002


Järvinen Petri <jarvin24 at lehtori.cc.tut.fi> wrote:
>Hello
>
>Is it possible to use dynamic binding in exception handling like in C++.
>
>I mean can I catch baseclass exception in my exception handler and get all 
>the derived classes catched aswell, like
>
>Class A:
>
>Class B(A):
>
>Class C(A):
>
>try:
> ...
>except A:
> (this would catch also B and C)
>


You are so close to being able to try it out yourself:

class A: pass

class B(A): pass

try:
    raise B()
except A, e:
    print "caught", e

caught <__main__.B instance at 0x816af4c>

Huaiyu



More information about the Python-list mailing list