Dune Core Modules (unstable)

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// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5
6#ifndef DUNE_EXCEPTIONS_HH
7#define DUNE_EXCEPTIONS_HH
8
9#include <exception>
10#include <string>
11#include <sstream>
12
13namespace Dune {
14
73 /* forward declarations */
74 class Exception;
75 struct ExceptionHook;
76
95 : public std::exception
96 {
97 public:
98 Exception ();
99 void message(const std::string &msg);
100 const char* what() const noexcept override;
101 static void registerHook (ExceptionHook * hook);
102 static void clearHook ();
103 private:
104 std::string _message;
105 static ExceptionHook * _hook;
106 };
107
174 {
175 virtual ~ExceptionHook() {}
176 virtual void operator () () = 0;
177 };
178
179 inline std::ostream& operator<<(std::ostream &stream, const Exception &e)
180 {
181 return stream << e.what();
182 }
183
184
196 template<class E>
198 : public E
199 {
200 public:
201 using E::E;
202
203 ExceptionStream(const E& other) :
204 E(other)
205 {}
206
207 ExceptionStream(E&& other) :
208 E(std::move(other))
209 {}
210
212 template<class T>
213 requires
214 (requires(std::ostringstream& oss, T t) { oss << t; }
215 and not std::is_integral_v<T>)
216 friend ExceptionStream& operator<<(ExceptionStream& es, const T& t)
217 {
218 es.sstream_ << t;
219 es.message(es.sstream_.str());
220 return es;
221 }
222
224 template<class T>
225 requires
226 (requires(std::ostringstream& oss, T t) { oss << t; }
227 and not std::is_integral_v<T>)
228 friend ExceptionStream operator<<(ExceptionStream&& es, const T& t)
229 {
230 es << t;
231 return std::move(es);
232 }
233
241 template<class T>
242 requires std::is_integral_v<T>
244 {
245 es.sstream_ << t;
246 es.message(es.sstream_.str());
247 return es;
248 }
249
251 template<class T>
252 requires std::is_integral_v<T>
254 {
255 es << t;
256 return std::move(es);
257 }
258
260 friend ExceptionStream& operator<<(ExceptionStream& es, std::ostream& (*t)(std::ostream&))
261 {
262 es.sstream_ << t;
263 es.message(es.sstream_.str());
264 return es;
265 }
266
268 friend ExceptionStream operator<<(ExceptionStream&& es, std::ostream& (*t)(std::ostream&))
269 {
270 es << t;
271 return std::move(es);
272 }
273
274 private:
275 std::ostringstream sstream_;
276 };
277
278
279
280#ifndef DOXYGEN
281 // the "format" the exception-type gets printed. __FILE__ and
282 // __LINE__ are standard C-defines, the GNU cpp-infofile claims that
283 // C99 defines __func__ as well. __FUNCTION__ is a GNU-extension
284#define THROWSPEC(E) # E << " [" << __func__ << ":" << __FILE__ << ":" << __LINE__ << "]: "
285#endif // DOXYGEN
286
312#define DUNE_THROW(E, ...) throw Dune::ExceptionStream(E()) << THROWSPEC(E) __VA_OPT__(<<) __VA_ARGS__
313
323 class IOError : public Exception {};
324
333 class MathError : public Exception {};
334
346 class RangeError : public Exception {};
347
355 class NotImplemented : public Exception {};
356
363 class SystemError : public Exception {};
364
368 class OutOfMemoryError : public SystemError {};
369
374
379 class ParallelError : public Exception {};
380
381} // end namespace
382
383#endif
Class for extending a Dune::Exception with a stream interface.
Definition: exceptions.hh:199
Base class for Dune-Exceptions.
Definition: exceptions.hh:96
Default exception class for I/O errors.
Definition: exceptions.hh:323
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:373
Default exception class for mathematical errors.
Definition: exceptions.hh:333
Default exception for dummy implementations.
Definition: exceptions.hh:355
Default exception if memory allocation fails.
Definition: exceptions.hh:368
Default exception if an error in the parallel communication of the program occurred.
Definition: exceptions.hh:379
Default exception class for range errors.
Definition: exceptions.hh:346
Default exception class for OS errors.
Definition: exceptions.hh:363
friend ExceptionStream operator<<(ExceptionStream &&es, std::ostream &(*t)(std::ostream &))
Stream operator for r-value ExceptionStream and io manipulator.
Definition: exceptions.hh:268
static void registerHook(ExceptionHook *hook)
add a functor which is called before a Dune::Exception is emitted (see Dune::ExceptionHook)
Definition: exceptions.cc:22
friend ExceptionStream operator<<(ExceptionStream &&es, T t)
Stream operator for r-value ExceptionStream and integral values.
Definition: exceptions.hh:253
friend ExceptionStream & operator<<(ExceptionStream &es, T t)
Stream operator for l-value ExceptionStream and integral values.
Definition: exceptions.hh:243
static void clearHook()
remove all hooks
Definition: exceptions.cc:27
void message(const std::string &msg)
store string in internal message buffer
Definition: exceptions.cc:32
friend ExceptionStream & operator<<(ExceptionStream &es, std::ostream &(*t)(std::ostream &))
Stream operator for l-value ExceptionStream and io manipulator.
Definition: exceptions.hh:260
const char * what() const noexcept override
output internal message buffer
Definition: exceptions.cc:37
Dune namespace.
Definition: alignedallocator.hh:13
Base class to add a hook to the Dune::Exception.
Definition: exceptions.hh:174
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Dec 21, 23:30, 2024)