Chaste Commit::f2ff7ee04e70ac9d06c57344df8d017dbb12b97b
Exception.hpp
Go to the documentation of this file.
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36
37#ifndef _EXCEPTION_HPP_
38#define _EXCEPTION_HPP_
39
45#include <string>
46#include <sstream>
47#include <cfloat> // For DBL_MAX
48#include <climits> // For UINT_MAX & INT_MAX, necessary in gcc-4.3
49#include <cstdlib> // Necessary in gcc-4.3 and later which don't include stdlib by default
50#include <exception>
51
53const unsigned UNSIGNED_UNSET = UINT_MAX;
55const int INT_UNSET = INT_MAX;
57const double DOUBLE_UNSET = DBL_MAX;
58
64class Exception : public std::runtime_error
65{
66public:
74 Exception(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
75
79 virtual ~Exception() = default;
80
86 std::string GetMessage() const;
87
93 std::string GetShortMessage() const;
94
104 std::string CheckShortMessage(const std::string& rExpected) const;
105
115 std::string CheckShortMessageContains(const std::string& rExpected) const;
116
125 static void Terminate(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
126
132 virtual const char* what() const noexcept override;
133
134protected:
143 void SetMessage(const std::string& rMessage,
144 const std::string& rFilename, unsigned lineNumber);
145
146private:
147 std::string mMessage;
148 std::string mShortMessage;
149};
150
156#define EXCEPTION(message) \
157 do { \
158 std::stringstream msg_stream; \
159 msg_stream << message; \
160 throw Exception(msg_stream.str(), __FILE__, __LINE__); \
161 } while (false)
162
163#include <boost/preprocessor/stringize.hpp>
164
171#define EXCEPT_IF_NOT(test) \
172 if (!(test)) EXCEPTION("Assertion tripped: " BOOST_PP_STRINGIZE(test))
173
174
181#define TERMINATE(message) \
182 do { \
183 std::stringstream msg_stream; \
184 msg_stream << message; \
185 Exception::Terminate(msg_stream.str(), __FILE__, __LINE__); \
186 } while (false)
187
219#define NEVER_REACHED TERMINATE("Should have been impossible to reach this line of code"); exit(EXIT_FAILURE)
220
226#ifdef NDEBUG
227#define UNUSED_OPT(var) var=var
228#else
229#define UNUSED_OPT(var)
230#endif
231
238#define ABORT_IF_THROWS(block) \
239 try { \
240 block; \
241 } catch (const Exception& e) { \
242 TERMINATE(e.GetMessage()); \
243 } catch (const std::exception &e) { \
244 TERMINATE(e.what()); \
245 } catch (...) { \
246 TERMINATE("Unexpected exception thrown."); \
247 }
248
249
250// The macros below are deprecated. In most cases, FileFinder routines should be used
251// in preference to system() calls.
252
267#define EXPECT0(cmd, arg) { \
268 std::string _arg(arg); \
269 int retcode = cmd(_arg.c_str()); \
270 if (retcode != 0) { \
271 EXCEPTION("Error executing command: " #cmd "(" + _arg + ")"); \
272 } }
273
274
283#define ABORT_IF_NON0_WITH_MSG(retcode, msg) \
284 if (retcode != 0) { \
285 TERMINATE(msg); \
286 }
287
300#define ABORT_IF_NON0(cmd, arg) { \
301 std::string _arg(arg); \
302 int ret = cmd(_arg.c_str()); \
303 ABORT_IF_NON0_WITH_MSG(ret, "Error executing command: " #cmd "(" + _arg + ")") \
304 }
305
314#define EXPECT_NON0(cmd, arg) { \
315 std::string _arg = (arg); \
316 int ret = cmd(_arg.c_str()); \
317 if (ret == 0) { \
318 EXCEPTION("Command: " #cmd "(" + _arg + ") succeeded and it shouldn't have"); \
319 } }
320
329#define IGNORE_RET(cmd, arg) { \
330 std::string _arg = (arg); \
331 int ret = cmd(_arg.c_str()); \
332 ret = ret; \
333 }
334
335#endif // _EXCEPTION_HPP_
const double DOUBLE_UNSET
Definition Exception.hpp:57
const int INT_UNSET
Definition Exception.hpp:55
const unsigned UNSIGNED_UNSET
Definition Exception.hpp:53
std::string GetMessage() const
Definition Exception.cpp:84
void SetMessage(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
Definition Exception.cpp:66
std::string mShortMessage
std::string CheckShortMessageContains(const std::string &rExpected) const
virtual ~Exception()=default
std::string mMessage
static void Terminate(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
virtual const char * what() const noexcept override
Definition Exception.cpp:94
std::string GetShortMessage() const
Definition Exception.cpp:89
std::string CheckShortMessage(const std::string &rExpected) const
Definition Exception.cpp:98