# How to catch errors in VTK

**URL:** https://discourse.vtk.org/t/how-to-catch-errors-in-vtk/1100
**Category:** Support
**Created:** [June 7, 2019, 7:44pm UTC](https://discourse.vtk.org/t/how-to-catch-errors-in-vtk/1100 "2019-06-07T19:44:01Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![kit](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/9fc29f/32.png) [@kit](https://discourse.vtk.org/u/kit)
#### Post date: [June 7, 2019, 7:44pm UTC](https://discourse.vtk.org/t/how-to-catch-errors-in-vtk/1100/1 "2019-06-07T19:44:01Z")

</div>

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 }`

---

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [June 7, 2019, 7:55pm UTC](https://discourse.vtk.org/t/how-to-catch-errors-in-vtk/1100/2 "2019-06-07T19:55:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [June 7, 2019, 8:02pm UTC](https://discourse.vtk.org/t/how-to-catch-errors-in-vtk/1100/3 "2019-06-07T20:02:29Z")

</div>

I usually report errors using vtkErrorMacro and capture them in tests using a custom [vtkTestingOutputWindow](https://github.com/Slicer/Slicer/blob/master/Libs/vtkAddon/vtkTestingOutputWindow.h).

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](https://github.com/Slicer/Slicer/blob/85f292af53eec404d659239926ed1acbd31236dc/Libs/MRML/Core/Testing/CMakeLists.txt#L3-L5).

We have hundreds of tests using this method, see for example [how expected errors are defined here](https://github.com/Slicer/Slicer/blob/85f292af53eec404d659239926ed1acbd31236dc/Libs/MRML/Core/Testing/vtkMRMLDisplayableHierarchyNodeTest3.cxx#L262-L268).

---

<div class="post-metadata">

### Author: ![lorensen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lorensen/32/20_2.png) [@lorensen](https://discourse.vtk.org/u/lorensen)
#### Post date: [June 7, 2019, 10:37pm UTC](https://discourse.vtk.org/t/how-to-catch-errors-in-vtk/1100/4 "2019-06-07T22:37:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![kit](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/k/9fc29f/32.png) [@kit](https://discourse.vtk.org/u/kit)
#### Post date: [June 10, 2019, 11:33am UTC](https://discourse.vtk.org/t/how-to-catch-errors-in-vtk/1100/5 "2019-06-10T11:33:32Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.vtk.org/u/flaviu2)
#### Post date: [August 19, 2020, 6:29am UTC](https://discourse.vtk.org/t/how-to-catch-errors-in-vtk/1100/6 "2020-08-19T06:29:07Z")

</div>

I think all links from this post: [How to catch errors in VTK](https://discourse.vtk.org/t/how-to-catch-errors-in-vtk/1100/3) seem to be broken.  
The first one might be this one: [vtkTestingOutputWindow](https://apidocs.slicer.org/v4.8/classvtkTestingOutputWindow.html)

And the latest might be this one: [https://github.com/Slicer/Slicer/blob/47deb76d7556e40de4e25e585c4b24a63a153da5/Libs/MRML/Core/Testing/vtkMRMLDisplayableHierarchyNodeTest3.cxx#L262](https://github.com/Slicer/Slicer/blob/47deb76d7556e40de4e25e585c4b24a63a153da5/Libs/MRML/Core/Testing/vtkMRMLDisplayableHierarchyNodeTest3.cxx#L262)

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [August 19, 2020, 9:48pm UTC](https://discourse.vtk.org/t/how-to-catch-errors-in-vtk/1100/7 "2020-08-19T21:48:09Z")

</div>

Git hashes are changed in Slicer/Slicer github repository. The original hashes are valid in this repository: [https://github.com/Slicer/SlicerGitSVNArchive](https://github.com/Slicer/SlicerGitSVNArchive)
