[FRPythoneers] trapping exceptions

Jim Vickroy Jim.Vickroy at noaa.gov
Wed May 21 11:32:37 MDT 2003


As I see it, the problem is that in order for your "except" block to work, a
"MyException" must be raised.  If its your code raising the exception, then you
can obviously raise a "MyException" and it would work as you intend.  But
standard, distribution Python code raises IOError exceptions which your except
block does not handle.

In general, I do not see how you can do anything but the following:

def change_state(parm):

except IOError, details:

Erick Bodine wrote:

> All,
> I would like to create a custom exception class that would wrap IOError
> so that I can catch its message string and do some other things.  I made
> a stab at it with the below code, trying several variations none of
> which were successful.  How is this accomplished and is there a better
> way?
> class MyException(IOError):
>         def __init__(self):
>                 change_state(0)
> def change_state(value):
>         # change a value in a temp file
> try:
>         f = open('nonexistent.txt')
> except MyException, error:
>         print "IOError message"
> --ERick
> _______________________________________________
> This message sent by the FRPythoneers mailing list.
> Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
> URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers

More information about the FRPythoneers mailing list