diff --git a/Core/Code/Common/mitkException.cpp b/Core/Code/Common/mitkException.cpp index 26e39bb838..5d6169997b 100644 --- a/Core/Code/Common/mitkException.cpp +++ b/Core/Code/Common/mitkException.cpp @@ -1,17 +1,43 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include "mitkException.h" + +void mitk::Exception::AddRethrowData(const char *file, unsigned int lineNumber, const char *message) + { + mitk::Exception::ReThrowData data = {file, lineNumber, message}; + m_RethrowData.push_back(data); + } + +int mitk::Exception::GetNumberOfRethrows() + { + return m_RethrowData.size(); + } + +void mitk::Exception::GetRethrowData(int rethrowNumber, std::string &file, int &line, std::string &message) + { + if (rethrowNumber > m_RethrowData.size()) + { + file = ""; + line = 0; + message = ""; + return; + } + + file = m_RethrowData.at(rethrowNumber).RethrowClassname; + line = m_RethrowData.at(rethrowNumber).RethrowLine; + message = m_RethrowData.at(rethrowNumber).RethrowMessage; + } diff --git a/Core/Code/Common/mitkException.h b/Core/Code/Common/mitkException.h index 56ceb03923..68cb7336a5 100644 --- a/Core/Code/Common/mitkException.h +++ b/Core/Code/Common/mitkException.h @@ -1,88 +1,107 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef MITKEXCEPTION_H_INCLUDED #define MITKEXCEPTION_H_INCLUDED #include #include namespace mitk { /**Documentation * \brief An object of this class represents an exception of MITK. * Please don't instantiate exceptions manually, but use the * exception macros (file mitkExceptionMacro.h) instead. * Simple use in your code is: * * mitkThrow() << "optional exception message"; * * You can also define specialized exceptions which must inherit * from this class. Please always use the mitkExceptionClassMacro * when implementing specialized exceptions. A simple implementation * can look like: * * class MyException : public mitk::Exception * { * public: * mitkExceptionClassMacro(MyException,mitk::Exception); * }; * * You can then throw your specialized exceptions by using the macro * * mitkThrowException(MyException) << "optional exception message"; */ class MITK_CORE_EXPORT Exception : public itk::ExceptionObject { public: Exception(const char *file, unsigned int lineNumber=0, const char *desc="None", const char *loc="Unknown") : itk::ExceptionObject(file,lineNumber,desc,loc){} virtual ~Exception() throw() {} itkTypeMacro(ClassName, SuperClassName); - + + /** \brief Adds rethrow data to this exception. */ + void AddRethrowData(const char *file, unsigned int lineNumber, const char *message); + + /** \return Returns how often the exception was rethrown. */ + int GetNumberOfRethrows(); + + /** \brief Returns the rethrow data of the specified rethrow number. */ + void GetRethrowData(int rethrowNumber, std::string &file, int &line, std::string &message); + /** \brief Definition of the bit shift operator for this class.*/ template inline Exception& operator<<(const T& data) { std::stringstream ss; ss << this->GetDescription() << data; this->SetDescription(ss.str()); return *this; } /** \brief Definition of the bit shift operator for this class (for non const data).*/ template inline Exception& operator<<(T& data) { std::stringstream ss; ss << this->GetDescription() << data; this->SetDescription(ss.str()); return *this; } /** \brief Definition of the bit shift operator for this class (for functions).*/ inline Exception& operator<<(std::ostream& (*func)(std::ostream&)) { std::stringstream ss; ss << this->GetDescription() << func; this->SetDescription(ss.str()); return *this; } + protected: + + struct ReThrowData + { + std::string RethrowClassname, + int RethrowLine, + std::string RethrowMessage + } + + std::vector m_RethrowData; }; } // namespace mitk #endif diff --git a/Core/Code/Common/mitkExceptionMacro.h b/Core/Code/Common/mitkExceptionMacro.h index cad5321ffa..6b5ae9cc39 100644 --- a/Core/Code/Common/mitkExceptionMacro.h +++ b/Core/Code/Common/mitkExceptionMacro.h @@ -1,76 +1,81 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #ifndef MITK_EXCEPTIONMACRO_H_DEFINED #define MITK_EXCEPTIONMACRO_H_DEFINED #include #include #include #include "mitkException.h" /** The exception macro is used to print error information / throw an exception * (i.e., usually a condition that results in program failure). * * Example usage looks like: * mitkThrow() << "this is error info"; */ #define mitkThrow() throw mitk::Exception(__FILE__,__LINE__,"",ITK_LOCATION) +//TODO implement and document +//#define mitkReThrow(mitkexception) \ +// throw mitkexception + + /** The specialized exception macro is used to print error information / throw exceptions * in cases of specialized errors. This means the second parameter must be a class which * inherits from mitk::Exception. An object of this exception is thrown when using the macro. * Thus, more differentiated excaptions can be thrown, when needed. * * Example usage: * mitkSpecializedExceptionMacro(mitk::MySpecializedException) << "this is error info"; */ #define mitkThrowException(classname) throw classname(__FILE__,__LINE__,"",ITK_LOCATION) /** Class macro for MITK exception classes. * All MITK exception classes should derive from MITK::Exception. */ #define mitkExceptionClassMacro(ClassName,SuperClassName) \ ClassName(const char *file, unsigned int lineNumber, const char *desc, const char *loc) :\ SuperClassName(file,lineNumber,desc,loc){}\ itkTypeMacro(ClassName, SuperClassName);\ /** \brief Definition of the bit shift operator for this class. It can be used to add messages.*/\ template inline ClassName& operator<<(const T& data)\ {\ std::stringstream ss;\ ss << this->GetDescription() << data;\ this->SetDescription(ss.str());\ return *this;\ }\ /** \brief Definition of the bit shift operator for this class (for non const data).*/\ template inline ClassName& operator<<(T& data)\ {\ std::stringstream ss;\ ss << this->GetDescription() << data;\ this->SetDescription(ss.str());\ return *this;\ }\ /** \brief Definition of the bit shift operator for this class (for functions).*/\ inline ClassName& operator<<(std::ostream& (*func)(std::ostream&))\ {\ std::stringstream ss;\ ss << this->GetDescription() << func;\ this->SetDescription(ss.str());\ return *this;\ }\ #endif