Re: How to call TPyException in Python?

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


Hello Wim,

Thank you for your detailed explanation. It makes sense. At the first time I read the HTML document, I thought that TPyException can be caught inside python. e.g.,

try:

    ROOT.throw_TPyException()
except ROOT.PyROOT.TPyException, e:

    # do something

That's is the reason why I was trying to access ROOT.PyROOT.TPyException in my test script.

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?

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/03, at 22:51, WLavrijsen_at_lbl.gov wrote:


> Akira,
>
>> What I want to do is to throw C++ exceptions from my C++ ROOT code, and >> catch them in PyROOT.
>
> that's fine, just throw any old exception, but preferably ones derived from
> std::exception b/c their what() can be called to transfer the error message.
> All C++ exceptions are turned into instances of Exception and can be caught.
>
> For example:
>
> % cat dothrow.C #include <stdexcept>
>
> void dothrow() {
> throw std::runtime_error( "hey there!" );
> }
>
> % python
>>>> import ROOT >>>> ROOT.gROOT.LoadMacro( 'dothrow.C+' )
> Info in <TUnixSystem::ACLiC>: creating shared library /Users/wlav/./dothrow_C.so
> 0
>>>> try:
> ... ROOT.dothrow()
> ... except Exception, e:
> ... error = e
> ...
>>>> print error
> hey there! (C++ exception)
>>>> ^D
>
> Perhaps the translation should be finer grained (e.g. std::runtime_error
> could have been RuntimeError), but no-one's ever asked for that. :)
>
>> ============== >> http://root.cern.ch/root/html/PyROOT__TPyException.html >> Purpose: A C++ exception class for throwing python exceptions >> through C++ code. >> ==============
>
> The important bit is "through", rather than "from." TPyException is used
> to get _python_ exceptions through C++ back into python. For example, if
> a python callable is handed to a TF1 for plotting on a canvas, and the
> callable then raises. That raised exception (python) is turned into a
> thrown TPyException (C++), caught, and turned back into a python exception
> (which is no work, really, since it's already set, but the return value is
> what matters).
>
>> Is there any tutorial of TPyException?
>
> No: there's only one use that I'm aware of and that's deep inside PyROOT.
>
> Best regards,
> Wim
> --
> WLavrijsen_at_lbl.gov -- +1 (510) 486 6411 -- www.lavrijsen.net
Received on Wed May 04 2011 - 20:07:46 CEST

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