How to catch errors in VTK

(Kit) #1

Are there some examples / best practices for catching errors in VTK?

The scenario I have is creating unit tests for a custom VTK filter, to make sure it catches cases where bad data is input, something like:

vtkSmartPointer<myFilter> filter = vtkSmartPointer<myFilter>::New();
filter->InputData( BadData );
filter->Update();
if(errorNotTriggered){ FailWithLotsOfNoise }

(Bill Lorensen) #2

This example may help…
https://lorensen.github.io/VTKExamples/site/Cxx/Utilities/ObserveError/

(Andras Lasso) #3

I usually report errors using vtkErrorMacro and capture them in tests using a custom vtkTestingOutputWindow.

The main difference compared to using an explicit error observer (as shown in @lorensen’s example above) is that using a custom output window you capture all errors from all filters and you do not need to add any extra code to your tests (unless you want to check for expected errors) - you just need to add a few extra lines into your CMakeLists.txt.

We have hundreds of tests using this method, see for example how expected errors are defined here.

(Bill Lorensen) #4

My main use of an error observer is to test error paths in VTK. I grab the error and verify that its contents contain certain strings.

For a big app like slicer, @lassoan approach is better.

1 Like
(Kit) #5

Thanks Andras/ Bill,

I originally tried the error observer method, and it works ok. I could catch errors in my filter but it still showed messages for Pipeline failure. The TestingOutputWindow approach seems to do what I want though.

Thanks again.

1 Like