dune-common  2.2.1
exceptions.hh
Go to the documentation of this file.
1 // -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=8 sw=2 sts=2:
3 // $Id: exceptions.hh 7077 2013-01-19 10:06:29Z mblatt $
4 
5 #ifndef DUNE_EXCEPTIONS_HH
6 #define DUNE_EXCEPTIONS_HH
7 
8 #include <string>
9 #include <sstream>
10 
11 namespace Dune {
12 
71 /* forward declarations */
72 class Exception;
73 struct ExceptionHook;
74 
92 class Exception {
93 public:
94  Exception ();
95  void message(const std::string &msg);
96  const std::string& what() const;
97  static void registerHook (ExceptionHook * hook);
98  static void clearHook ();
99 private:
100  std::string _message;
101  static ExceptionHook * _hook;
102 };
103 
170 {
171  virtual ~ExceptionHook() {}
172  virtual void operator () () = 0;
173 };
174 
175 /*
176  Implementation of Dune::Exception
177  */
178 
180 {
181  // call the hook if necessary
182  if (_hook != 0) _hook->operator()();
183 }
184 
186 {
187  _hook = hook;
188 }
189 
190 inline void Exception::clearHook ()
191 {
192  _hook = 0;
193 }
194 
195 inline void Exception::message(const std::string & msg)
196 {
197  _message = msg;
198 }
199 
200 inline const std::string& Exception::what() const
201 {
202  return _message;
203 }
204 
205 inline std::ostream& operator<<(std::ostream &stream, const Exception &e)
206 {
207  return stream << e.what();
208 }
209 
210 #ifndef DOXYGEN
211 // the "format" the exception-type gets printed. __FILE__ and
212 // __LINE__ are standard C-defines, the GNU cpp-infofile claims that
213 // C99 defines __func__ as well. __FUNCTION__ is a GNU-extension
214 #define THROWSPEC(E) #E << " [" << __func__ << ":" << __FILE__ << ":" << __LINE__ << "]: "
215 #endif // DOXYGEN
216 
242 // this is the magic: use the usual do { ... } while (0) trick, create
243 // the full message via a string stream and throw the created object
244 #define DUNE_THROW(E, m) do { E th__ex; std::ostringstream th__out; \
245  th__out << THROWSPEC(E) << m; th__ex.message(th__out.str()); throw th__ex; \
246  } while (0)
247 
257 class IOError : public Exception {};
258 
267 class MathError : public Exception {};
268 
280 class RangeError : public Exception {};
281 
289 class NotImplemented : public Exception {};
290 
297 class SystemError : public Exception {};
298 
302 class OutOfMemoryError : public SystemError {};
303 
308 
313 class ParallelError : public Exception {};
314 
315 } // end namespace
316 
317 #endif