Dune Core Modules (2.4.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
4#ifndef DUNE_EXCEPTIONS_HH
5#define DUNE_EXCEPTIONS_HH
6
7#include <string>
8#include <sstream>
9
10namespace Dune {
11
70 /* forward declarations */
71 class Exception;
72 struct ExceptionHook;
73
91 class Exception {
92 public:
93 Exception ();
94 void message(const std::string &msg);
95 const std::string& what() const;
96 static void registerHook (ExceptionHook * hook);
97 static void clearHook ();
98 private:
99 std::string _message;
100 static ExceptionHook * _hook;
101 };
102
169 {
170 virtual ~ExceptionHook() {}
171 virtual void operator () () = 0;
172 };
173
174 /*
175 Implementation of Dune::Exception
176 */
177
178 inline Exception::Exception ()
179 {
180 // call the hook if necessary
181 if (_hook != 0) _hook->operator()();
182 }
183
185 {
186 _hook = hook;
187 }
188
189 inline void Exception::clearHook ()
190 {
191 _hook = 0;
192 }
193
194 inline void Exception::message(const std::string & msg)
195 {
196 _message = msg;
197 }
198
199 inline const std::string& Exception::what() const
200 {
201 return _message;
202 }
203
204 inline std::ostream& operator<<(std::ostream &stream, const Exception &e)
205 {
206 return stream << e.what();
207 }
208
209#ifndef DOXYGEN
210 // the "format" the exception-type gets printed. __FILE__ and
211 // __LINE__ are standard C-defines, the GNU cpp-infofile claims that
212 // C99 defines __func__ as well. __FUNCTION__ is a GNU-extension
213#define THROWSPEC(E) # E << " [" << __func__ << ":" << __FILE__ << ":" << __LINE__ << "]: "
214#endif // DOXYGEN
215
241 // this is the magic: use the usual do { ... } while (0) trick, create
242 // the full message via a string stream and throw the created object
243#define DUNE_THROW(E, m) do { E th__ex; std::ostringstream th__out; \
244 th__out << THROWSPEC(E) << m; th__ex.message(th__out.str()); throw th__ex; \
245} while (0)
246
256 class IOError : public Exception {};
257
266 class MathError : public Exception {};
267
279 class RangeError : public Exception {};
280
288 class NotImplemented : public Exception {};
289
296 class SystemError : public Exception {};
297
301 class OutOfMemoryError : public SystemError {};
302
307
312 class ParallelError : public Exception {};
313
314} // end namespace
315
316#endif
Base class for Dune-Exceptions.
Definition: exceptions.hh:91
Default exception class for I/O errors.
Definition: exceptions.hh:256
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:306
Default exception class for mathematical errors.
Definition: exceptions.hh:266
Default exception for dummy implementations.
Definition: exceptions.hh:288
Default exception if memory allocation fails.
Definition: exceptions.hh:301
Default exception if an error in the parallel communication of the programm occured.
Definition: exceptions.hh:312
Default exception class for range errors.
Definition: exceptions.hh:279
Default exception class for OS errors.
Definition: exceptions.hh:296
std::ostream & operator<<(std::ostream &s, const array< T, N > &e)
Output operator for array.
Definition: array.hh:26
static void registerHook(ExceptionHook *hook)
add a functor which is called before a Dune::Exception is emitted (see Dune::ExceptionHook)
Definition: exceptions.hh:184
static void clearHook()
remove all hooks
Definition: exceptions.hh:189
const std::string & what() const
output internal message buffer
Definition: exceptions.hh:199
void message(const std::string &msg)
store string in internal message buffer
Definition: exceptions.hh:194
Dune namespace.
Definition: alignment.hh:10
Base class to add a hook to the Dune::Exception.
Definition: exceptions.hh:169
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 22, 23:30, 2024)