My Project
Loading...
Searching...
No Matches
CriticalError.hpp
Go to the documentation of this file.
1/*
2 Copyright 2025 Equinor ASA
3
4 This file is part of the Open Porous Media project (OPM).
5 OPM is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 OPM is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with OPM. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19#ifndef OPM_COMMON_CRITICAL_ERROR_HPP
20#define OPM_COMMON_CRITICAL_ERROR_HPP
21#include <exception>
22#include <iostream>
23#include <stdexcept>
24#include <string>
25
26namespace Opm
65{
73class CriticalError : public std::exception
74{
75public:
82 explicit CriticalError(const std::string_view& message,
83 std::exception_ptr inner_exception = nullptr)
84 : m_message(message)
85 , m_innerException(inner_exception)
86 {
87 }
88
89
95 const char* what() const noexcept override
96 {
97 return m_message.c_str();
98 }
99
105 std::exception_ptr getInnerException() const
106 {
107 return m_innerException;
108 }
109
110private:
111 std::string m_message;
112 std::exception_ptr m_innerException;
113};
114
115namespace detail
116{
117 inline std::string makeCriticalErrorMessageErrorHint(const std::string& text)
118 {
119 return std::string("\nError hint: ") + text;
120 }
121
122 inline std::string makeCriticalErrorMessageErrorHint()
123 {
124 return std::string("");
125 }
126} // namespace detail
127
136#define OPM_CATCH_AND_RETHROW_AS_CRITICAL_ERROR(...) \
137 catch (const ::Opm::CriticalError&) \
138 { \
139 throw; \
140 } \
141 catch (const std::exception& exp) \
142 { \
143 const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
144 + std::to_string(__LINE__) + "].\nOriginal error: " + exp.what() \
145 + ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
146 throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
147 } \
148 catch (...) \
149 { \
150 const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
151 + std::to_string(__LINE__) + "]. Unknown original error." \
152 + ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
153 throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
154 }
155
162#define OPM_TRY_THROW_AS_CRITICAL_ERROR(expr, ...) \
163 try { \
164 expr; \
165 } catch (const ::Opm::CriticalError&) { \
166 throw; \
167 } catch (const std::exception& exp) { \
168 const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
169 + std::to_string(__LINE__) + "].\nOriginal error: " + exp.what() \
170 + ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
171 throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
172 } catch (...) { \
173 const auto messageForCriticalError = std::string("Error rethrown as CriticalError at [") + __FILE__ + ":" \
174 + std::to_string(__LINE__) + "]. Unknown original error." \
175 + ::Opm::detail::makeCriticalErrorMessageErrorHint(__VA_ARGS__); \
176 throw ::Opm::CriticalError(messageForCriticalError, std::current_exception()); \
177 }
178
179} // namespace Opm
180#endif // OPM_COMMON_CRITICAL_ERROR_HPP
A custom exception class that extends std::exception to handle critical errors.
Definition CriticalError.hpp:74
CriticalError(const std::string_view &message, std::exception_ptr inner_exception=nullptr)
Constructs a CriticalError with a specified message and an optional inner exception.
Definition CriticalError.hpp:82
std::exception_ptr getInnerException() const
Retrieves the inner exception.
Definition CriticalError.hpp:105
const char * what() const noexcept override
Returns the error message.
Definition CriticalError.hpp:95
This class implements a small container which holds the transmissibility mulitpliers for all the face...
Definition Exceptions.hpp:30