Re: How to call TPyException in Python?

From: OKUMURA, Akira <oxon_at_astro.isas.jaxa.jp>
Date: Wed, 4 May 2011 11:50:29 -0700


Hello Wim,

> but I guess
> what you want is a downcast of the exception to the actual type, check
> whether there's a dictionary available and then turn it into that type on
> the python side?

Yes. If I can know the type of the exception, my code will be, for example, as follows.

try:

    ROOT.GetDataFromMyDetector()
except ROOT.MyExceptionWriteError, e:

    print "Cannot write data on the disk"     # cont...
except ROOT.MyExceptionNoResponse, e:

    print "No response from the detector"     # cont...
except:

    print "Unknown error"
    # cont...

But in the case of Exception, it will be if-elif instead. I prefer the above script.

try:

    ROOT.GetDataFromMyDetector()
except Exception, e:

    if e.message == "Cannot write data on the disk":

        print e
        # cont...
    elif e.message == "No response from the detector"
        print e
        # cont...
    elif:
        print "Unknown error"
        # cont...

> Not impossible, just scary to put a lot of work inside a catch block of
> an exception handler. :} (If something else goes wrong and there is a
> second exception, the application would terminate.)

Are you talking about end-user-side work or dev-team-side work?

Regards,

--
OKUMURA, Akira oxon@{astro.isas.jaxa.jp,stanford.edu}
Institute of Space and Astronautical Science (ISAS/JAXA)
Now at KIPAC/SLAC/Stanford
Varian Physics #306, 382 Via Pueblo Mall, MC 4060
Stanford, CA 94305-4060
TEL 650-736-0971/FAX 650-724-5065
Skype : okumura.akira

On 2011/05/04, at 11:19, wlavrijsen_at_lbl.gov wrote:


> Akira,
>
>> According to your explanation, it seems that my own exception classes >> derived from std::exception are not useful because they are all turned >> into single class, Exception. Therefore, if I want to distinguish different >> type of errors (exceptions) from each other, I always have to check >> Exception.message in if statement instead of try statement. Am I correct?
>
> that is the case. I can fix that for the known exceptions that map nicely
> onto python ones, and I can probably even add the type name, but I guess
> what you want is a downcast of the exception to the actual type, check
> whether there's a dictionary available and then turn it into that type on
> the python side?
>
> Not impossible, just scary to put a lot of work inside a catch block of
> an exception handler. :} (If something else goes wrong and there is a
> second exception, the application would terminate.)
>
> Best regards,
> Wim
> --
> WLavrijsen_at_lbl.gov -- +1 (510) 486 6411 -- www.lavrijsen.net
Received on Wed May 04 2011 - 20:50:43 CEST

This archive was generated by hypermail 2.2.0 : Wed May 04 2011 - 23:50:01 CEST