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#include <utility>
14
16
17namespace autopas::utils {
18
39};
40
47 public:
52 static void setBehavior(ExceptionBehavior behavior);
53
63 template <class Exception>
64 static void exception(const Exception e) {
65 std::lock_guard<std::mutex> guard(exceptionMutex);
66 switch (_behavior) {
67 case throwException:
68 throw e; // NOLINT
69 default:
70 nonThrowException(e);
71 }
72 }
73
87 template <typename First, typename... Args>
88 static void exception(fmt::format_string<First, Args...> exceptionString, First &&first, Args &&...args);
89
96 static void rethrow();
97
102 static void setCustomAbortFunction(std::function<void()> function);
103
104 private:
105 static std::mutex exceptionMutex;
106 static ExceptionBehavior _behavior;
107 static std::function<void()> _customAbortFunction;
108
109 static void nonThrowException(const std::exception &e);
110
111 public:
116 class AutoPasException : public std::exception {
117 public:
122 explicit AutoPasException(std::string description);
123
129
130 ~AutoPasException() override;
131
136 [[nodiscard]] const char *what() const noexcept override;
137
138 private:
139 std::string _description;
140 };
141};
142
147template <>
148void ExceptionHandler::exception(const std::string e); // NOLINT
149
154template <>
155void ExceptionHandler::exception(const char *const e); // NOLINT
156
157template <typename First, typename... Args>
158void ExceptionHandler::exception(fmt::format_string<First, Args...> exceptionString, First &&first, Args &&...args) {
159 std::string s = fmt::format(exceptionString, std::forward<First>(first), std::forward<Args>(args)...);
160 exception(s);
161}
162
163} // namespace autopas::utils
Default exception class for autopas exceptions.
Definition: ExceptionHandler.h:116
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:46
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:64
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:22
@ printCustomAbortFunction
Print the exception and call a custom abort function.
Definition: ExceptionHandler.h:38
@ ignore
Ignore all exceptions.
Definition: ExceptionHandler.h:26
@ printAbort
Print the exception and.
Definition: ExceptionHandler.h:34
@ throwException
Throw the exception.
Definition: ExceptionHandler.h:30