Exception.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * NICE-Core - efficient algebra and computer vision methods
  3. * - libfbasics - library of some basic tools
  4. * See file License for license information.
  5. */
  6. #ifndef _FEXCEPTION_H_
  7. #define _FEXCEPTION_H_
  8. #include <exception>
  9. #include <string>
  10. #include <iostream>
  11. #include <sstream>
  12. namespace NICE {
  13. /**
  14. * Throw an exception of type \c type with message \c m
  15. * and supply the exception's constructor with source code file and line.
  16. * The message will be composed using a stream.
  17. * Example: <code>fthrow(Exception, "ups. error: " << code)</code>
  18. */
  19. #define fthrow(type, stream) \
  20. { \
  21. std::stringstream sneudfgiaenuiae; \
  22. sneudfgiaenuiae << stream; \
  23. throw type((__FILE__), (__LINE__), sneudfgiaenuiae.str()); \
  24. };
  25. /**
  26. * Throw an exception of type \c type
  27. * with message \c m and causing exception \c cause
  28. * and supply the exception's constructor with source code file and line.
  29. * Additionally specify the causing exception (when re-throwing).
  30. * The message will be composed using a stream.
  31. * Example: <code>fthrowc(Exception, "ups. error: " << code, cause)</code>
  32. */
  33. #define fthrowc(type, stream, cause) \
  34. {using namespace NICE; \
  35. std::stringstream sneudfgiaenuiae; \
  36. sneudfgiaenuiae << stream; \
  37. throw type((__FILE__), (__LINE__), sneudfgiaenuiae.str(), (cause)); \
  38. };
  39. /**
  40. * @deprecated Same as fthrow
  41. */
  42. #define fthrowstream(type, m) \
  43. fthrow(type, m)
  44. // {using namespace NICE; throw type((__FILE__), (__LINE__), (m));};
  45. /**
  46. * @deprecated Same as fthrowc
  47. */
  48. #define fthrowstreamc(type, m, cause) \
  49. fthrowc(type, m, cause)
  50. // {using namespace NICE; throw type((__FILE__), (__LINE__), (m), (cause));};
  51. /**
  52. * A general purpose exception class.
  53. * There are additional macros \c ::fthrow und \c ::fthrowc which simplify
  54. * the usage of some features of this class.
  55. */
  56. class Exception : public std::exception {
  57. public:
  58. /**
  59. * Create an exception with a message string.
  60. * @param _message The message text
  61. */
  62. inline Exception(const std::string& _message)
  63. : filename("unknown"), line(0), message(_message) {}
  64. /**
  65. * Create an exception with a message string and a causing exception
  66. * @param _message The message text
  67. * @param _cause The causing exception
  68. */
  69. Exception(const std::string& _message, const exception& _cause);
  70. /**
  71. * Create an exception with a message string and information about the
  72. * throwing position in the source code.
  73. * @param _filename Source code file
  74. * @param _line Source code line
  75. * @param _message The message text
  76. */
  77. inline Exception(const char* _filename, const int _line,
  78. const std::string& _message)
  79. : filename(_filename), line(_line), message(_message) {}
  80. /**
  81. * Create an exception with a message string and a causing exception
  82. * as well as information about the throwing position in the source code.
  83. * @param _filename Source code file
  84. * @param _line Source code line
  85. * @param _message The message text
  86. * @param _cause The causing exception
  87. */
  88. Exception(const char* _filename, const int _line,
  89. const std::string& _message, const exception& _cause);
  90. inline Exception(const Exception& e) throw()
  91. : std::exception(e) {
  92. *this = e;
  93. }
  94. Exception& operator=(const Exception& e) throw() {
  95. filename = e.filename;
  96. line = e.line;
  97. message = e.message;
  98. cause = e.cause;
  99. return *this;
  100. }
  101. virtual ~Exception() throw();
  102. /**
  103. * The message text and additional information. Useful as output message.
  104. */
  105. virtual const char* what() const throw();
  106. /**
  107. * The name of the exception class. Should be re-implemented in subclasses.
  108. */
  109. virtual const char* name() const throw();
  110. protected:
  111. //! Source code file
  112. std::string filename;
  113. //! Source code line
  114. int line;
  115. //! Message text
  116. std::string message;
  117. //! Causing exception
  118. std::string cause;
  119. //! a buffer for the string returned by what()
  120. mutable std::string whatBuffer;
  121. };
  122. } // namespace
  123. /**
  124. * Define new exception classes.
  125. * Example: <code>DEFINE_NICE_EXCEPTION(ImageException)</code>.
  126. */
  127. #define DEFINE_NICE_EXCEPTION(ename) \
  128. class ename : public NICE::Exception { \
  129. public: \
  130. inline ename(const std::string& _message) : Exception(_message) {} \
  131. inline ename(const std::string& _message, const exception& _cause) \
  132. : Exception(_message, _cause) {} \
  133. inline ename(const char* _filename, const int _line, \
  134. const std::string& _message) \
  135. : Exception(_filename, _line, _message) {} \
  136. inline ename(const char* _filename, const int _line, \
  137. const std::string& _message, const exception& _cause) \
  138. : Exception(_filename, _line, _message, _cause) {} \
  139. inline ename(const ename& orig) throw() : NICE::Exception(orig) {} \
  140. inline ename& operator=(const ename& orig) throw() { \
  141. *this = orig; \
  142. return *this; \
  143. } \
  144. virtual ~ename() throw() {} \
  145. const char* name() const throw() { return #ename; } \
  146. };
  147. #define DEFINE_LIMUN_EXCEPTION(ename) DEFINE_NICE_EXCEPTION(ename)
  148. /*
  149. #define LIMUN_EXCEPTION_IMPLEMENTATION(ename) \
  150. ename::~ename() throw() { \
  151. } \
  152. const char* ename::name() const throw() { \
  153. return #ename; \
  154. }
  155. */
  156. #endif //_FEXCEPTION_H_