dune-common  2.3.1
exceptions.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 // $Id$
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 
307  class InvalidStateException : public Exception {};
308 
313  class ParallelError : public Exception {};
314 
315 } // end namespace
316 
317 #endif
Default exception for dummy implementations.
Definition: exceptions.hh:289
Default exception class for I/O errors.
Definition: exceptions.hh:257
static void registerHook(ExceptionHook *hook)
add a functor which is called before a Dune::Exception is emitted (see Dune::ExceptionHook) ...
Definition: exceptions.hh:185
Default exception class for mathematical errors.
Definition: exceptions.hh:267
const std::string & what() const
output internal message buffer
Definition: exceptions.hh:200
Base class to add a hook to the Dune::Exception.
Definition: exceptions.hh:169
static void clearHook()
remove all hooks
Definition: exceptions.hh:190
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:307
virtual ~ExceptionHook()
Definition: exceptions.hh:171
Default exception if an error in the parallel communication of the programm occured.
Definition: exceptions.hh:313
Exception()
Definition: exceptions.hh:179
Default exception class for OS errors.
Definition: exceptions.hh:297
virtual void operator()()=0
std::ostream & operator<<(std::ostream &s, const array< T, N > &e)
Output operator for array.
Definition: array.hh:159
Base class for Dune-Exceptions.
Definition: exceptions.hh:92
void message(const std::string &msg)
store string in internal message buffer
Definition: exceptions.hh:195
Default exception class for range errors.
Definition: exceptions.hh:280
Default exception if memory allocation fails.
Definition: exceptions.hh:302