Handle errors on SimpleITK

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:

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

Flaviu.

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

Hello,

The exception object you want to catch is the sitk::GenericException. It is derived from std::exception.

2 Likes

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

1 Like