How to catch errors in VTK

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 );
if(errorNotTriggered){ FailWithLotsOfNoise }

This example may help…

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.

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

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

I think all links from this post: How to catch errors in VTK seem to be broken.
The first one might be this one: vtkTestingOutputWindow

And the latest might be this one:

1 Like

Git hashes are changed in Slicer/Slicer github repository. The original hashes are valid in this repository: