Chaste  Release::3.4
Exception.hpp
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, 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 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF 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 
52 const unsigned UNSIGNED_UNSET = UINT_MAX;
54 const int INT_UNSET = INT_MAX;
56 const double DOUBLE_UNSET = DBL_MAX;
57 
63 class Exception
64 {
65 public:
73  Exception(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
74 
80  std::string GetMessage() const;
81 
87  std::string GetShortMessage() const;
88 
98  std::string CheckShortMessage(std::string expected) const;
99 
109  std::string CheckShortMessageContains(std::string expected) const;
110 
119  static void Terminate(const std::string& rMessage, const std::string& rFilename, unsigned lineNumber);
120 
121 protected:
130  void SetMessage(const std::string& rMessage,
131  const std::string& rFilename, unsigned lineNumber);
132 
133 private:
134  std::string mMessage;
135  std::string mShortMessage;
136 };
137 
143 #define EXCEPTION(message) \
144  do { \
145  std::stringstream msg_stream; \
146  msg_stream << message; \
147  throw Exception(msg_stream.str(), __FILE__, __LINE__); \
148  } while (false)
149 
150 #include <boost/preprocessor/stringize.hpp>
151 
158 #define EXCEPT_IF_NOT(test) \
159  if (!(test)) EXCEPTION("Assertion tripped: " BOOST_PP_STRINGIZE(test))
160 
161 
168 #define TERMINATE(message) \
169  do { \
170  std::stringstream msg_stream; \
171  msg_stream << message; \
172  Exception::Terminate(msg_stream.str(), __FILE__, __LINE__); \
173  } while (false)
174 
206 #define NEVER_REACHED TERMINATE("Should have been impossible to reach this line of code"); exit(EXIT_FAILURE)
207 
213 #ifdef NDEBUG
214 #define UNUSED_OPT(var) var=var
215 #else
216 #define UNUSED_OPT(var)
217 #endif
218 
225 #define ABORT_IF_THROWS(block) \
226  try { \
227  block; \
228  } catch (const Exception& e) { \
229  TERMINATE(e.GetMessage()); \
230  } catch (const std::exception &e) { \
231  TERMINATE(e.what()); \
232  } catch (...) { \
233  TERMINATE("Unexpected exception thrown."); \
234  }
235 
236 
237 // The macros below are deprecated. In most cases, FileFinder routines should be used
238 // in preference to system() calls.
239 
254 #define EXPECT0(cmd, arg) { \
255  std::string _arg(arg); \
256  int retcode = cmd(_arg.c_str()); \
257  if (retcode != 0) { \
258  EXCEPTION("Error executing command: " #cmd "(" + _arg + ")"); \
259  } }
260 
261 
270 #define ABORT_IF_NON0_WITH_MSG(retcode, msg) \
271  if (retcode != 0) { \
272  TERMINATE(msg); \
273  }
274 
287 #define ABORT_IF_NON0(cmd, arg) { \
288  std::string _arg(arg); \
289  int ret = cmd(_arg.c_str()); \
290  ABORT_IF_NON0_WITH_MSG(ret, "Error executing command: " #cmd "(" + _arg + ")") \
291  }
292 
301 #define EXPECT_NON0(cmd, arg) { \
302  std::string _arg = (arg); \
303  int ret = cmd(_arg.c_str()); \
304  if (ret == 0) { \
305  EXCEPTION("Command: " #cmd "(" + _arg + ") succeeded and it shouldn't have"); \
306  } }
307 
316 #define IGNORE_RET(cmd, arg) { \
317  std::string _arg = (arg); \
318  int ret = cmd(_arg.c_str()); \
319  ret = ret; \
320  }
321 
322 #endif // _EXCEPTION_HPP_
std::string CheckShortMessage(std::string expected) const
Definition: Exception.cpp:97
Exception(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
Definition: Exception.cpp:52
const double DOUBLE_UNSET
Definition: Exception.hpp:56
void SetMessage(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
Definition: Exception.cpp:65
std::string mMessage
Definition: Exception.hpp:134
std::string GetMessage() const
Definition: Exception.cpp:87
const unsigned UNSIGNED_UNSET
Definition: Exception.hpp:52
static void Terminate(const std::string &rMessage, const std::string &rFilename, unsigned lineNumber)
Definition: Exception.cpp:118
std::string mShortMessage
Definition: Exception.hpp:135
std::string CheckShortMessageContains(std::string expected) const
Definition: Exception.cpp:107
std::string GetShortMessage() const
Definition: Exception.cpp:92
const int INT_UNSET
Definition: Exception.hpp:54