# Handle errors on SimpleITK

**URL:** https://discourse.itk.org/t/handle-errors-on-simpleitk/2698
**Category:** Uncategorized
**Tags:** simpleitk
**Created:** [February 10, 2020, 12:16pm UTC](https://discourse.itk.org/t/handle-errors-on-simpleitk/2698 "2020-02-10T12:16:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)
#### Post date: [February 10, 2020, 12:16pm UTC](https://discourse.itk.org/t/handle-errors-on-simpleitk/2698/1 "2020-02-10T12:16:12Z")

</div>

Can you tell me how to handle possible errors when I run simpleitk code ?

Here is an example:

```
#include "sitkImage.h"
#include "sitkCurvatureFlowImageFilter.h"
#include "sitkConnectedThresholdImageFilter.h"
#include "sitkImageFileReader.h"
#include "sitkImageFileWriter.h"

#include <stdlib.h>
#include <iostream>

namespace sitk = itk::simple;

int main(int argc, char* argv[])
{
	//
	// Read the image
	//
	sitk::ImageFileReader reader;
	reader.SetFileName("d:\\tempx\\3DSlice1.dcm");
	sitk::Image image = reader.Execute();
	//
	// Blur using CurvatureFlowImageFilter
	//
	sitk::CurvatureFlowImageFilter blurFilter;
	blurFilter.SetNumberOfIterations(5);
	blurFilter.SetTimeStep(0.125);
	image = blurFilter.Execute(image);
	//
	// Set up ConnectedThresholdImageFilter for segmentation
	//
	sitk::ConnectedThresholdImageFilter segmentationFilter;
	segmentationFilter.SetLower(1600);
	segmentationFilter.SetUpper(2400);
	segmentationFilter.SetReplaceValue(255);

//	for (int i = 5; i + 1 < argc; i += 2)
	{
		std::vector<unsigned int> seed;
		seed.push_back(50);
		seed.push_back(100);
		segmentationFilter.AddSeed(seed);
		std::cout << "Adding a seed at: ";
		for (unsigned int j = 0; j < seed.size(); ++j)
		{
			std::cout << seed[j] << " ";
		}
		std::cout << std::endl;
	}

	sitk::Image outImage = segmentationFilter.Execute(image);

	//
	// Write out the resulting file
	//
	sitk::ImageFileWriter writer;
	writer.SetFileName("d:\\tempx\\3DSlice1XXX.dcm");
	writer.Execute(outImage);

	return 0;
}

```

at line

```
sitk::Image outImage = segmentationFilter.Execute(image);

```

my VS2017 jumpt to:

 ![image](https://discourse.itk.org/uploads/default/original/2X/5/5fad3ffbcf1fe8ea3db666478ee6550dc1e947cc.png)

in real world this should not gonna happen … I have to handle somehow the errors … how can I do that ?

Flaviu.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)
#### Post date: [February 10, 2020, 12:20pm UTC](https://discourse.itk.org/t/handle-errors-on-simpleitk/2698/2 "2020-02-10T12:20:15Z")

</div>

Of course, I have tried:

```
sitk::Image outImage;

try
{
	outImage = segmentationFilter.Execute(image);
}
catch (itk::ExceptionObject& exception)
{

}

```

but is not compile:  
`1>D:\Project\ConnectedThresholdImageFilter\ConnectedThresholdImageFilter.cpp(80): error C2316: 'itk::ExceptionObject &': cannot be caught as the destructor and/or copy constructor are inaccessible or deleted`

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [February 10, 2020, 3:07pm UTC](https://discourse.itk.org/t/handle-errors-on-simpleitk/2698/3 "2020-02-10T15:07:42Z")

</div>

Hello,

The exception object you want to catch is the [`sitk::GenericException`](https://github.com/SimpleITK/SimpleITK/blob/master/Code/Common/include/sitkMacro.h#L92). It is derived from std::exception.

---

<div class="post-metadata">

### Author: ![flaviu2](https://discourse.itk.org/letter_avatar_proxy/v4/letter/f/74df32/32.png) [@flaviu2](https://discourse.itk.org/u/flaviu2)
#### Post date: [February 10, 2020, 3:41pm UTC](https://discourse.itk.org/t/handle-errors-on-simpleitk/2698/4 "2020-02-10T15:41:00Z")

</div>

Yes, this class solved my issue. Thank you !!!
