[vtkusers] Re: Catching vtkError in python

Charl P. Botha c.p.botha at ewi.tudelft.nl
Sat Aug 2 18:42:28 EDT 2003


Mathieu, I'm forwarding my reply to VTK users, as it might be useful to 
others.

Mathieu Malaterre wrote:
>     I have seen your post:
> 
> http://public.kitware.com/pipermail/vtk-developers/2003-March/002358.html
> 
>     But I still don't understand how I can use it in a :
> 
> try:
>     ...
> except ErrorEvent:
>     ...
> 
> block. Could you please send me a small sample ?

The VTK Python observer-based error handling doesn't follow this 
pattern.  If you add an ErrorEvent observer to an object, and that 
observer (Python callable object) has a valid CallDataType attribute, it 
will be called with the errormessage as parameter.  Please see the 
attached example.  You need a relatively recent CVS for this, as I only 
committed it quite a while after the posting you quote above.

Unfortunately, due to the way in which Python wrapping works, manually 
raising an exception in your ErrorEvent observer also won't work: you'll 
get an immediate printout of the exception that you raise, but you won't 
be able to catch it.  This is because the wrapped function that's being 
called by Python (in our case "reader.Update()") returns a valid result, 
so Python doesn't know that something has gone wrong and doesn't query 
the current exception.  Due to this, the wrapping system calls 
PyErr_Print() so that you at least get to see what went wrong.  See 
this: http://www.python.org/dev/doc/devel/ext/callingPython.html  to 
understand why it has to be this way.  VTK doesn't make use of C++ 
exceptions, so it's incredibly difficult for the error information to 
propagate all the way back to the actual function that is being called 
by Python.

Because ITK makes use of C++ exceptions, we were able to re-propagate 
ALL errors back to Python as Python exceptions.  VTK does NOT make use 
of C++ exceptions for good reasons.  Maybe some of the developers have 
other ideas about this... it would for instance be interesting to see 
what would happen if the vtkErrorMacro were modified to throw an 
exception instead of invoking a VTK event.  Developers?

Personally, I think both error handling systems have their merits, 
although in this case and especially in the case of VTK <-> ITK 
pipelines that have been connected in scripts, C++ exception-based error 
handling might be more defensible.

HTH,
Charl

-- 
charl p. botha http://cpbotha.net/ http://visualisation.tudelft.nl/
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: errorTest.py
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20030802/218e1ea1/attachment.txt>


More information about the vtkusers mailing list