[vtkusers] Catch Exceptions of vtkSTLReader

Mathieu Malaterre mathieu.malaterre at gmail.com
Thu Feb 26 09:43:31 EST 2009


On Thu, Feb 26, 2009 at 1:43 AM, Eugene Guo <yguo2006 at gmail.com> wrote:
> Hi,
>
> I am trying to catch the exception by vtkSTLReader (e.g. to read a non-exist
> STL file), so that the vtkOutputWindow will not show up. I added two
> observer events (vtkCommand::ErrorEvent and vtkCommand::WarningEvent) to the
> reader, then use Try...Catch. It looks that the error event was caught,
> since the error message was printed on the screen; but the vtkOutputWindow
> is also displayed while the "Update()" is executed.
>
>     vtkMyObserver *errObserver = vtkMyObserver::New();
>
>     vtkSTLReader    *stlReader;
>     stlReader = vtkSTLReader::New();
>     stlReader->SetFileName("abc.stl"); // random name which will be sure to
> throw an exception
>     stlReader->AddObserver(vtkCommand::ErrorEvent,errObserver);
>     stlReader->AddObserver(vtkCommand::WarningEvent,errObserver);
>     stlReader->AddObserver(vtkCommand::AbortCheckEvent,errObserver);
>
>     try {
>     stlReader->Update();
>     }
>     catch (...){
>     }
>
>     if (errObserver->GetVTKError()) {
>         cout << "Caught an vtkSTLReader exception \n";
>         return false;
>     }
>
> However, the same sequence of code works perfectly for vtkPolyDataReader. I
> only saw the error message, while the vtkOutputWindow was not shown at all.
>
>    vtkMyObserver *errObserver = vtkMyObserver::New();
>
>     vtkPolyDataReader    *vtkReader;
>     vtkReader = vtkSTLReader::New();
>     vtkReader->SetFileName("abc.vtk"); // random name which will be sure to
> throw an exception
>     vtkReader->AddObserver(vtkCommand::ErrorEvent,errObserver);
>     vtkReader->AddObserver(vtkCommand::WarningEvent,errObserver);
>     vtkReader->AddObserver(vtkCommand::AbortCheckEvent,errObserver);
>
>     try {
>     stlReader->Update();
>     }
>     catch (...){
>     }
>
>     if (errObserver->GetVTKError()) {
>         cout << "Caught an vtkPolyDataReader exception \n";
>         return false;
>     }
>
>
> Any comments or suggestions are appreciated.

VTK does not throw exception, so you do not need your try/catch.

I can see the vtkErrorMacro in the vtkSTLReader. It is simply missing
the corresponding SetErrorCode (I could fix that, but VTK CVS write
access is currently restricted).
Meanwhile do not rely on error code, but instead simply use the
default mechanism of command/observer on ErrorEvent (vtkErrorMacro
does invoke it).

2cts
-- 
Mathieu



More information about the vtkusers mailing list