Heidelberg Educational Numerics Library  Version 0.24 (from 9 September 2011)
exceptions.hh
Go to the documentation of this file.
1 #ifndef HDNUM_EXCEPTIONS_HH
2 #define HDNUM_EXCEPTIONS_HH
3 
4 #include <string>
5 #include <sstream>
6 
7 namespace hdnum {
8 
35  class Exception {
36  public:
37  void message(const std::string &message);
38  const std::string& what() const;
39  private:
40  std::string _message;
41  };
42 
43  inline void Exception::message(const std::string &message)
44  {
45  _message = message;
46  }
47 
48  inline const std::string& Exception::what() const
49  {
50  return _message;
51  }
52 
53  inline std::ostream& operator<<(std::ostream &stream, const Exception &e)
54  {
55  return stream << e.what();
56  }
57 
58  // the "format" the exception-type gets printed. __FILE__ and
59  // __LINE__ are standard C-defines, the GNU cpp-infofile claims that
60  // C99 defines __func__ as well. __FUNCTION__ is a GNU-extension
61 #ifdef HDNUM_DEVEL_MODE
62 # define THROWSPEC(E) #E << " [" << __func__ << ":" << __FILE__ << ":" << __LINE__ << "]: "
63 #else
64 # define THROWSPEC(E) #E << ": "
65 #endif
66 
84  // this is the magic: use the usual do { ... } while (0) trick, create
85  // the full message via a string stream and throw the created object
86 #define HDNUM_THROW(E, m) do { E th__ex; std::ostringstream th__out; \
87  th__out << THROWSPEC(E) << m; th__ex.message(th__out.str()); throw th__ex; \
88  } while (0)
89 
99  class IOError : public Exception {};
100 
109  class MathError : public Exception {};
110 
122  class RangeError : public Exception {};
123 
131  class NotImplemented : public Exception {};
132 
139  class SystemError : public Exception {};
140 
144  class OutOfMemoryError : public SystemError {};
145 
149  class InvalidStateException : public Exception {};
150 
153  class ErrorException : public Exception {};
154 
155  // throw ErrorException with message
156 #define HDNUM_ERROR(m) do { hdnum::ErrorException th__ex; std::ostringstream th__out; \
157  th__out << THROWSPEC(hdnum::ErrorException) << m; \
158  th__ex.message(th__out.str()); \
159  std::cout << th__ex.what() << std::endl; \
160  throw th__ex; \
161  } while (0)
162 
163 } // end namespace
164 
165 #endif
Default exception class for I/O errors.
Definition: exceptions.hh:99
Base class for Exceptions.
Definition: exceptions.hh:35
Default exception class for range errors.
Definition: exceptions.hh:122
void message(const std::string &message)
store string in internal message buffer
Definition: exceptions.hh:43
Default exception for dummy implementations.
Definition: exceptions.hh:131
Default exception class for OS errors.
Definition: exceptions.hh:139
General Error.
Definition: exceptions.hh:153
const std::string & what() const
output internal message buffer
Definition: exceptions.hh:48
Default exception class for mathematical errors.
Definition: exceptions.hh:109
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:149
Default exception if memory allocation fails.
Definition: exceptions.hh:144
Definition: densematrix.hh:21