Dune Core Modules (2.9.0)

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 (C) 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#ifndef DOXYGEN
185 // the "format" the exception-type gets printed. __FILE__ and
186 // __LINE__ are standard C-defines, the GNU cpp-infofile claims that
187 // C99 defines __func__ as well. __FUNCTION__ is a GNU-extension
188#define THROWSPEC(E) # E << " [" << __func__ << ":" << __FILE__ << ":" << __LINE__ << "]: "
189#endif // DOXYGEN
190
216 // this is the magic: use the usual do { ... } while (0) trick, create
217 // the full message via a string stream and throw the created object
218#define DUNE_THROW(E, m) do { E th__ex; std::ostringstream th__out; \
219 th__out << THROWSPEC(E) << m; th__ex.message(th__out.str()); throw th__ex; \
220} while (0)
221
231 class IOError : public Exception {};
232
241 class MathError : public Exception {};
242
254 class RangeError : public Exception {};
255
263 class NotImplemented : public Exception {};
264
271 class SystemError : public Exception {};
272
276 class OutOfMemoryError : public SystemError {};
277
282
287 class ParallelError : public Exception {};
288
289} // end namespace
290
291#endif
Base class for Dune-Exceptions.
Definition: exceptions.hh:96
Default exception class for I/O errors.
Definition: exceptions.hh:231
Default exception if a function was called while the object is not in a valid state for that function...
Definition: exceptions.hh:281
Default exception class for mathematical errors.
Definition: exceptions.hh:241
Default exception for dummy implementations.
Definition: exceptions.hh:263
Default exception if memory allocation fails.
Definition: exceptions.hh:276
Default exception if an error in the parallel communication of the program occurred.
Definition: exceptions.hh:287
Default exception class for range errors.
Definition: exceptions.hh:254
Default exception class for OS errors.
Definition: exceptions.hh:271
static void registerHook(ExceptionHook *hook)
add a functor which is called before a Dune::Exception is emitted (see Dune::ExceptionHook)
Definition: exceptions.cc:22
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
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 29, 23:29, 2024)