From: dsarrut Date: Mon, 4 Oct 2010 07:46:52 +0000 (+0000) Subject: error are now handled by exception X-Git-Tag: v1.2.0~376 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=c7f6f682ddc01d2a41740bbdb806f9ef967b57d5;p=clitk.git error are now handled by exception --- diff --git a/common/clitkCommon.cxx b/common/clitkCommon.cxx index d3f6016..7afeb07 100644 --- a/common/clitkCommon.cxx +++ b/common/clitkCommon.cxx @@ -240,7 +240,7 @@ void clitk::openFileForReading(std::ifstream & is, const std::string & filename) { is.open(filename.c_str(), std::ios::in); if ( is.fail() ) { - itkGenericExceptionMacro(<< "Could not open file (for reading): " << filename); + clitkExceptionMacro("Could not open file (for reading): " << filename); } } //-------------------------------------------------------------------- @@ -251,7 +251,7 @@ void clitk::openFileForWriting(std::ofstream & os, const std::string & filename) { os.open(filename.c_str(), std::ios::out); if ( os.fail() ) { - itkGenericExceptionMacro(<< "Could not open file (for writing): " << filename); + clitkExceptionMacro("Could not open file (for writing): " << filename); } } //-------------------------------------------------------------------- diff --git a/common/clitkCommon.h b/common/clitkCommon.h index 476c0f1..879d54f 100644 --- a/common/clitkCommon.h +++ b/common/clitkCommon.h @@ -22,6 +22,7 @@ // clitk include #include "clitkConfiguration.h" #include "clitkPortability.h" +#include "clitkExceptionObject.h" // itk include (include std) #include diff --git a/common/clitkCommon.txx b/common/clitkCommon.txx index d7c9140..b5a3471 100644 --- a/common/clitkCommon.txx +++ b/common/clitkCommon.txx @@ -17,17 +17,11 @@ ======================================================================-====*/ #ifndef CLITKCOMMON_TXX #define CLITKCOMMON_TXX -/** - ------------------------------------------------- - * @file clitkCommon.txx - * @author David Sarrut - * @date 18 May 2006 - * - -------------------------------------------------*/ //------------------------------------------------------- // Utility functions for text file parsing (author: joel schaerer) +//-------------------------------------------------------------------- template ElementType parse_value(std::string str) { @@ -37,7 +31,10 @@ ElementType parse_value(std::string str) assert(!parser.fail()); return value; } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- template std::vector parse_string(std::string str,char delim) { @@ -49,7 +46,10 @@ std::vector parse_string(std::string str,char delim) } return result; } +//-------------------------------------------------------------------- + +//-------------------------------------------------------------------- template std::vector > parse_file(const char* filename,char delim) { @@ -62,6 +62,8 @@ std::vector > parse_file(const char* filename,char deli } return result; } +//-------------------------------------------------------------------- + //-------------------------------------------------------------------- // Convert float, double ... to string diff --git a/common/clitkFilterBase.cxx b/common/clitkFilterBase.cxx index 07b9925..0f7e852 100644 --- a/common/clitkFilterBase.cxx +++ b/common/clitkFilterBase.cxx @@ -22,34 +22,12 @@ //-------------------------------------------------------------------- clitk::FilterBase::FilterBase() { - SetMustStop(false); SetVerboseOption(false); SetCurrentStepNumber(0); SetCurrentStepBaseId(""); - StopOnErrorOn(); - ResetLastError(); VerboseWarningOffOn(); // OffOn, it's cool no ? SetWarning(""); -} -//-------------------------------------------------------------------- - - -//-------------------------------------------------------------------- -void clitk::FilterBase::ResetLastError() -{ - m_LastError = ""; -} -//-------------------------------------------------------------------- - - -//-------------------------------------------------------------------- -void clitk::FilterBase::SetLastError(std::string e) -{ - m_LastError = e; - if (GetStopOnError()) { - std::cerr << GetLastError() << std::endl; - exit(0); - } + m_IsCancelled = false; } //-------------------------------------------------------------------- @@ -68,8 +46,10 @@ void clitk::FilterBase::SetWarning(std::string e) //-------------------------------------------------------------------- void clitk::FilterBase::StartNewStep(std::string s) { - //m_CurrentStepTimer.Reset(); - // m_CurrentStepTimer.Start(); + if (Cancelled()) { + throw clitk::ExceptionObject("Filter is canceled."); + } + m_CurrentStepNumber++; if (GetCurrentStepBaseId() != "") { std::ostringstream oss; @@ -94,33 +74,23 @@ void clitk::FilterBase::StartNewStep(std::string s) //-------------------------------------------------------------------- void clitk::FilterBase::StopCurrentStep() { - // m_CurrentStepTimer.Stop(); - // m_CurrentStepTimer.Print(std::cout); - // std::ostringstream oss; - //oss << " (" << - // m_CurrentStepName = m_CurrentStepName +" + } //-------------------------------------------------------------------- //-------------------------------------------------------------------- -void clitk::FilterBase::SetMustStop(bool b) +void clitk::FilterBase::Cancel() { - m_MustStop = b; - if (GetMustStop()) { - SetLastError("Filter is interrupted."); - } + m_IsCancelled = true; } //-------------------------------------------------------------------- //-------------------------------------------------------------------- -bool clitk::FilterBase::GetMustStop() -{ - if (m_MustStop) return true; - if (HasError()) return true; - return false; -} + bool clitk::FilterBase::Cancelled() + { + return m_IsCancelled; + } //-------------------------------------------------------------------- - diff --git a/common/clitkFilterBase.h b/common/clitkFilterBase.h index a53a181..1cd7579 100644 --- a/common/clitkFilterBase.h +++ b/common/clitkFilterBase.h @@ -32,10 +32,11 @@ namespace clitk { //-------------------------------------------------------------------- /* - Convenient class to manage options from GGO gengetopt) to filter + Convenient class to manage options from GGO (gengetopt) to filter */ //-------------------------------------------------------------------- - class FilterBase { + class FilterBase + { public: // Standard class typedefs @@ -90,15 +91,6 @@ namespace clitk { template void VerboseOptionV(std::string name, int nb, OptionType * value); - // Error - void SetLastError(std::string e); - void ResetLastError(); - itkGetConstMacro(LastError, std::string); - bool HasError() { return (GetLastError() != ""); } - itkSetMacro(StopOnError, bool); - itkGetConstMacro(StopOnError, bool); - itkBooleanMacro(StopOnError); - void SetWarning(std::string e); itkGetConstMacro(Warning, std::string); itkSetMacro(VerboseWarningOff, bool); @@ -106,10 +98,10 @@ namespace clitk { itkBooleanMacro(VerboseWarningOff); GGO_DefineOption_Flag(verboseWarningOff, SetVerboseWarningOff); - // Use this function to stop (when threaded) - void SetMustStop(bool b); - bool GetMustStop(); - + // Use this function to cancel the filter between step + void Cancel(); + bool Cancelled(); + protected: FilterBase(); virtual ~FilterBase() {} @@ -125,13 +117,12 @@ namespace clitk { int m_NumberOfSteps; std::string m_CurrentStepId; std::string m_CurrentStepBaseId; - std::string m_LastError; std::string m_CurrentStepName; - bool m_StopOnError; std::string m_Warning; bool m_VerboseWarningOff; - bool m_MustStop; + bool m_IsCancelled; Timer m_CurrentStepTimer; + private: FilterBase(const Self&); //purposely not implemented @@ -143,8 +134,6 @@ namespace clitk { } // end namespace clitk //-------------------------------------------------------------------- -#define StartNewStepOrStop(s) StartNewStep(s); if (GetMustStop()) return; - #ifndef ITK_MANUAL_INSTANTIATION #include "clitkFilterBase.txx" #endif diff --git a/common/clitkImageCommon.cxx b/common/clitkImageCommon.cxx index d3cd8d8..9367257 100644 --- a/common/clitkImageCommon.cxx +++ b/common/clitkImageCommon.cxx @@ -14,32 +14,22 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html -======================================================================-====*/ + ======================================================================-====*/ + #ifndef CLITKIMAGECOMMON_CXX #define CLITKIMAGECOMMON_CXX -/** - ------------------------------------------------= - * @file clitkImageCommon.cxx - * @author David Sarrut - * @date 02 Oct 2007 14:30:47 - * - * @brief - * - * - ------------------------------------------------=*/ #include "clitkImageCommon.h" //-------------------------------------------------------------------- void clitk::ReadImageDimensionAndPixelType(const std::string & filename, - int & dimension, - std::string & pixeType) + int & dimension, + std::string & pixeType) { itk::ImageIOBase::Pointer genericReader = itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode); if (!genericReader) { - std::cerr << "Image file format unknown while reading file <" << filename << ">" << std::endl; - exit(0); + clitkExceptionMacro("Image file format unknown while reading file <" << filename << ">"); } genericReader->SetFileName(filename.c_str()); genericReader->ReadImageInformation(); @@ -51,14 +41,13 @@ void clitk::ReadImageDimensionAndPixelType(const std::string & filename, //-------------------------------------------------------------------- void clitk::ReadImageDimensionAndPixelType(const std::string & filename, - int & dimension, - std::string & pixeType, int & components) + int & dimension, + std::string & pixeType, int & components) { itk::ImageIOBase::Pointer genericReader = itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode); if (!genericReader) { - std::cerr << "Image file format unknown while reading " << filename << std::endl; - exit(0); + clitkExceptionMacro("Image file format unknown while reading file <" << filename << ">"); } genericReader->SetFileName(filename.c_str()); genericReader->ReadImageInformation(); @@ -91,10 +80,8 @@ itk::ImageIOBase::Pointer clitk::readImageHeader(const std::string & filename, b itk::ImageIOFactory::CreateImageIO(filename.c_str(), itk::ImageIOFactory::ReadMode); if (!reader) { if (exit_on_error) { //default behavior for tools who don't handle the problem - std::cerr << "Error reading file " << filename << ", exiting immediately" << std::endl; - std::exit(-1); - } else - return NULL; + clitkExceptionMacro("Error reading file " << filename << ", exiting immediately"); + } else return NULL; } reader->SetFileName(filename); reader->ReadImageInformation(); diff --git a/common/clitkImageToImageGenericFilterBase.cxx b/common/clitkImageToImageGenericFilterBase.cxx index 259ae04..70dcba6 100644 --- a/common/clitkImageToImageGenericFilterBase.cxx +++ b/common/clitkImageToImageGenericFilterBase.cxx @@ -34,8 +34,8 @@ clitk::ImageToImageGenericFilterBase::ImageToImageGenericFilterBase(std::string m_FilterName = n; m_FailOnImageTypeError = true; m_ReadOnDisk = true; - m_LastError = ""; - StopOnErrorOn(); + // m_LastError = ""; + // StopOnErrorOn(); SetFilterBase(NULL); } //-------------------------------------------------------------------- @@ -310,12 +310,12 @@ typename ImageType::Pointer clitk::ImageToImageGenericFilterBase::GetInput(unsig //-------------------------------------------------------------------- -void clitk::ImageToImageGenericFilterBase::MustStop() -{ - if (m_FilterBase != NULL) { - m_FilterBase->SetMustStop(true); - } -} +// void clitk::ImageToImageGenericFilterBase::MustStop() +// { +// if (m_FilterBase != NULL) { +// m_FilterBase->SetMustStop(true); +// } +// } //-------------------------------------------------------------------- diff --git a/common/clitkImageToImageGenericFilterBase.h b/common/clitkImageToImageGenericFilterBase.h index aa61904..86cd77a 100644 --- a/common/clitkImageToImageGenericFilterBase.h +++ b/common/clitkImageToImageGenericFilterBase.h @@ -54,9 +54,9 @@ namespace clitk { void SetFilterName(std::string & n); // Error management - itkSetMacro(LastError, std::string); - itkGetConstMacro(LastError, std::string); - bool HasError() { return (GetLastError() != ""); } + // itkSetMacro(LastError, std::string); + // itkGetConstMacro(LastError, std::string); + // bool HasError() { return (GetLastError() != ""); } // Generic IO /// Returns the dimension and pixel type of the *first* input @@ -94,11 +94,11 @@ namespace clitk { FilterBase * GetFilterBase() { return m_FilterBase; } // Indicate that the filter must stop as soon as possible (if threaded) - void MustStop(); + // void MustStop(); void DeleteLastOutputImage(); - itkSetMacro(StopOnError, bool); - itkGetConstMacro(StopOnError, bool); - itkBooleanMacro(StopOnError); + // itkSetMacro(StopOnError, bool); + // itkGetConstMacro(StopOnError, bool); + // itkBooleanMacro(StopOnError); protected: bool m_ReadOnDisk; @@ -126,10 +126,10 @@ namespace clitk { void SetImageTypeError(); bool m_FailOnImageTypeError; - std::string m_LastError; + // std::string m_LastError; void SetFilterBase(FilterBase * f) { m_FilterBase = f; } FilterBase * m_FilterBase; - bool m_StopOnError; + // bool m_StopOnError; }; // end class clitk::ImageToImageGenericFilter diff --git a/segmentation/clitkConnectedComponentLabeling.cxx b/segmentation/clitkConnectedComponentLabeling.cxx index 620c5c4..bc2fcb1 100644 --- a/segmentation/clitkConnectedComponentLabeling.cxx +++ b/segmentation/clitkConnectedComponentLabeling.cxx @@ -33,10 +33,10 @@ int main(int argc, char * argv[]) FilterType::Pointer filter = FilterType::New(); filter->SetArgsInfo(args_info); - filter->Update(); - - if (filter->HasError()) { - std::cout << filter->GetLastError() << std::endl; + try { + filter->Update(); + } catch(std::runtime_error e) { + std::cout << e.what() << std::endl; } return EXIT_SUCCESS; diff --git a/segmentation/clitkExtractBones.cxx b/segmentation/clitkExtractBones.cxx index 2122fc7..64cc29b 100644 --- a/segmentation/clitkExtractBones.cxx +++ b/segmentation/clitkExtractBones.cxx @@ -33,7 +33,12 @@ int main(int argc, char * argv[]) FilterType::Pointer filter = FilterType::New(); filter->SetArgsInfo(args_info); - filter->Update(); + + try { + filter->Update(); + } catch(std::runtime_error e) { + std::cout << e.what() << std::endl; + } return EXIT_SUCCESS; } // This is the end, my friend diff --git a/segmentation/clitkExtractBonesGenericFilter.txx b/segmentation/clitkExtractBonesGenericFilter.txx index 58f00f8..dd85634 100644 --- a/segmentation/clitkExtractBonesGenericFilter.txx +++ b/segmentation/clitkExtractBonesGenericFilter.txx @@ -82,13 +82,6 @@ void clitk::ExtractBonesGenericFilter::UpdateWithInputImageType() // Go ! filter->Update(); - // Check if error - if (filter->HasError()) { - SetLastError(filter->GetLastError()); - // No output - return; - } - // Write/Save results typename OutputImageType::Pointer output = filter->GetOutput(); this->template SetNextOutput(output); diff --git a/segmentation/clitkExtractPatient.cxx b/segmentation/clitkExtractPatient.cxx index 8e78809..027a62c 100644 --- a/segmentation/clitkExtractPatient.cxx +++ b/segmentation/clitkExtractPatient.cxx @@ -32,10 +32,11 @@ int main(int argc, char * argv[]) { FilterType::Pointer filter = FilterType::New(); filter->SetArgsInfo(args_info); - filter->Update(); - - if (filter->HasError()) { - std::cout << filter->GetLastError() << std::endl; + + try { + filter->Update(); + } catch(std::runtime_error e) { + std::cout << e.what() << std::endl; } return EXIT_SUCCESS; diff --git a/segmentation/clitkExtractPatientGenericFilter.txx b/segmentation/clitkExtractPatientGenericFilter.txx index f90c1c7..7fc0740 100644 --- a/segmentation/clitkExtractPatientGenericFilter.txx +++ b/segmentation/clitkExtractPatientGenericFilter.txx @@ -82,12 +82,12 @@ void clitk::ExtractPatientGenericFilter::UpdateWithInputImageType( // Go ! filter->Update(); - // Check if error - if (filter->HasError()) { - SetLastError(filter->GetLastError()); - // No output - return; - } + // // Check if error + // if (filter->HasError()) { + // SetLastError(filter->GetLastError()); + // // No output + // return; + // } // Write/Save results typename OutputImageType::Pointer output = filter->GetOutput(); diff --git a/tools/clitkAutoCrop.cxx b/tools/clitkAutoCrop.cxx index 93d0e01..e22fa6a 100644 --- a/tools/clitkAutoCrop.cxx +++ b/tools/clitkAutoCrop.cxx @@ -33,10 +33,11 @@ int main(int argc, char * argv[]) FilterType::Pointer filter = FilterType::New(); filter->SetArgsInfo(args_info); - filter->Update(); - if (filter->HasError()) { - std::cout << filter->GetLastError() << std::endl; + try { + filter->Update(); + } catch(std::runtime_error e) { + std::cout << e.what() << std::endl; } return EXIT_SUCCESS;