Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
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
179#define EXCEPT_IF(test) \
180 if (test) EXCEPTION("Assertion tripped: " BOOST_PP_STRINGIZE(test))
181
182
189#define TERMINATE(message) \
190 do { \
191 std::stringstream msg_stream; \
192 msg_stream << message; \
193 Exception::Terminate(msg_stream.str(), __FILE__, __LINE__); \
194 } while (false)
195
227#define NEVER_REACHED TERMINATE("Should have been impossible to reach this line of code"); exit(EXIT_FAILURE)
228
234#ifdef NDEBUG
235#define UNUSED_OPT(var) var=var
236#else
237#define UNUSED_OPT(var)
238#endif
239
246#define ABORT_IF_THROWS(block) \
247 try { \
248 block; \
249 } catch (const Exception& e) { \
250 TERMINATE(e.GetMessage()); \
251 } catch (const std::exception &e) { \
252 TERMINATE(e.what()); \
253 } catch (...) { \
254 TERMINATE("Unexpected exception thrown."); \
255 }
256
257
258// The macros below are deprecated. In most cases, FileFinder routines should be used
259// in preference to system() calls.
260
275#define EXPECT0(cmd, arg) { \
276 std::string _arg(arg); \
277 int retcode = cmd(_arg.c_str()); \
278 if (retcode != 0) { \
279 EXCEPTION("Error executing command: " #cmd "(" + _arg + ")"); \
280 } }
281
282
291#define ABORT_IF_NON0_WITH_MSG(retcode, msg) \
292 if (retcode != 0) { \
293 TERMINATE(msg); \
294 }
295
308#define ABORT_IF_NON0(cmd, arg) { \
309 std::string _arg(arg); \
310 int ret = cmd(_arg.c_str()); \
311 ABORT_IF_NON0_WITH_MSG(ret, "Error executing command: " #cmd "(" + _arg + ")") \
312 }
313
322#define EXPECT_NON0(cmd, arg) { \
323 std::string _arg = (arg); \
324 int ret = cmd(_arg.c_str()); \
325 if (ret == 0) { \
326 EXCEPTION("Command: " #cmd "(" + _arg + ") succeeded and it shouldn't have"); \
327 } }
328
337#define IGNORE_RET(cmd, arg) { \
338 std::string _arg = (arg); \
339 int ret = cmd(_arg.c_str()); \
340 ret = ret; \
341 }
342
343#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