AutoPas  3.0.0
Loading...
Searching...
No Matches
ExceptionHandler.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <cstdlib>
10#include <functional>
11#include <mutex>
12#include <string>
13
15
16namespace autopas::utils {
17
38};
39
46 public:
51 static void setBehavior(ExceptionBehavior behavior);
52
62 template <class Exception>
63 static void exception(const Exception e) {
64 std::lock_guard<std::mutex> guard(exceptionMutex);
65 switch (_behavior) {
66 case throwException:
67 throw e; // NOLINT
68 default:
69 nonThrowException(e);
70 }
71 }
72
86 template <typename First, typename... Args>
87 static void exception(std::string exceptionString, First first, Args... args); // recursive variadic function
88
95 static void rethrow();
96
101 static void setCustomAbortFunction(std::function<void()> function);
102
103 private:
104 static std::mutex exceptionMutex;
105 static ExceptionBehavior _behavior;
106 static std::function<void()> _customAbortFunction;
107
108 static void nonThrowException(const std::exception &e);
109
110 public:
115 class AutoPasException : public std::exception {
116 public:
121 explicit AutoPasException(std::string description);
122
128
129 ~AutoPasException() override;
130
135 [[nodiscard]] const char *what() const noexcept override;
136
137 private:
138 std::string _description;
139 };
140};
141
146template <>
147void ExceptionHandler::exception(const std::string e); // NOLINT
148
153template <>
154void ExceptionHandler::exception(const char *const e); // NOLINT
155
156template <typename First, typename... Args>
157void ExceptionHandler::exception(std::string exceptionString, First first, Args... args) {
158 std::string s = fmt::format(exceptionString, first, args...);
159 exception(s);
160}
161
162} // namespace autopas::utils
Default exception class for autopas exceptions.
Definition: ExceptionHandler.h:115
AutoPasException(const AutoPasException &exception)
Copy constructor.
const char * what() const noexcept override
returns the description
Definition: ExceptionHandler.cpp:87
Defines and handles the throwing and printing of exceptions.
Definition: ExceptionHandler.h:45
static void setCustomAbortFunction(std::function< void()> function)
Set a custom abort function.
Definition: ExceptionHandler.cpp:50
static void rethrow()
Rethrows the current exception or prints it.
Definition: ExceptionHandler.cpp:27
static void setBehavior(ExceptionBehavior behavior)
Set the behavior of the handler.
Definition: ExceptionHandler.cpp:45
static void exception(const Exception e)
Handle an exception derived by std::exception.
Definition: ExceptionHandler.h:63
In this namespace some helper classes and functions can be found used inside of AutoPas.
Definition: namespaces.h:44
ExceptionBehavior
Enum that defines the behavior of the expection handling.
Definition: ExceptionHandler.h:21
@ printCustomAbortFunction
Print the exception and call a custom abort function.
Definition: ExceptionHandler.h:37
@ ignore
Ignore all exceptions.
Definition: ExceptionHandler.h:25
@ printAbort
Print the exception and.
Definition: ExceptionHandler.h:33
@ throwException
Throw the exception.
Definition: ExceptionHandler.h:29