MADNESS  0.10.1
madness_exception.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 */
31 
32 /**
33  \file madness_exception.h
34  \brief Defines \c madness::MadnessException for exception handling.
35  \ingroup libraries
36 
37  By default, the \c MADNESS_ASSERT macro throws a \c madness::MadnessException.
38  Configure options can specify other behaviors: \c MADNESS_ASSERT can be
39  - THROW: (default) throw a \c madness::MadnessException.
40  - ASSERT: defer to the standard \c assert function.
41  - DISABLE: do nothing (ignore the assertion).
42  - ABORT: abort execution.
43 */
44 
45 #ifndef MADNESS_WORLD_MADNESS_EXCEPTION_H__INCLUDED
46 #define MADNESS_WORLD_MADNESS_EXCEPTION_H__INCLUDED
47 
48 #include <cstdlib>
49 #include <iosfwd>
50 #include <exception>
51 #include <madness/madness_config.h>
52 #ifdef MADNESS_ASSERTIONS_ASSERT
53 # include <cassert>
54 #endif
55 
56 #ifndef MADNESS_DISPLAY_EXCEPTION_BREAK_MESSAGE
57 /// Display the exception break message unless otherwise specified.
58 #define MADNESS_DISPLAY_EXCEPTION_BREAK_MESSAGE 1
59 #endif
60 
61 namespace madness {
62 
63  /// Base class for exceptions thrown in MADNESS.
64 
65  /// Most exceptions thrown in MADNESS should be derived from this.
66  class MadnessException : public std::exception {
67  public:
68  const char* msg; ///< The error message.
69  const char* assertion; ///< String describing the assertion.
70  const int value; ///< Value associated with the exception.
71  const int line; ///< Line number where the exception occurred.
72  const char *function; ///< Function where the exception occurred.
73  const char *filename; ///< File where the exception occurred.
74 
75  /// Constructor that processes the requisite information.
76 
77  /// Capturing the line/function/filename info is best done with the
78  /// macros listed below.
79  /// \param[in] m The error message.
80  /// \param[in] a String describing the exception.
81  /// \param[in] v Value associated with the exception.
82  /// \param[in] l Line number where the exception occurred.
83  /// \param[in] fn Function where the exception occurred.
84  /// \param[in] f File where the exception occurred.
85  MadnessException(const char* m, const char *a, int v,
86  int l, const char *fn, const char *f)
87  : msg(m)
88  , assertion(a)
89  , value(v)
90  , line(l)
91  , function(fn)
92  , filename(f) {}
93 
94  /// Returns the error message, as specified by `std::exception`.
95 
96  /// \return The error message.
97  virtual const char* what() const throw() {
98  return msg;
99  }
100  };
101 
102  /// Enables easy printing of a \c MadnessException.
103 
104  /// \param[in,out] out Output stream.
105  /// \param[in] e The \c MadnessException.
106  /// \return The output stream.
107  std::ostream& operator <<(std::ostream& out, const MadnessException& e);
108 
109  /// This function is executed just before a \c MadnessException is thrown.
110 
111  /// \param[in] message True to print an error message to \c cerr; false otherwise.
112  void exception_break(bool message);
113 
114 /// Macro for throwing a MADNESS exception.
115 
116 /// \throws A \c madness::MadnessException.
117 /// \param[in] msg The error message.
118 /// \param[in] value The value associated with the exception.
119 #define MADNESS_EXCEPTION(msg,value) { \
120  madness::exception_break(MADNESS_DISPLAY_EXCEPTION_BREAK_MESSAGE); \
121  throw madness::MadnessException(msg,0,value,__LINE__,__FUNCTION__,__FILE__); \
122 }
123 
124 // the following define/undef are for documentation purposes only.
125 /// Assert a condition that should be free of side-effects since in release builds this might be a no-op.
126 
127 /// Depending on the configuration, one of the following happens if
128 /// \c condition is false:
129 /// - a \c madness::MadnessException is thrown.
130 /// - `assert(condition)` is called.
131 /// - execution is aborted.
132 /// - nothing.
133 /// \param[in] condition The condition to be asserted.
134 #define MADNESS_ASSERT(condition)
135 #undef MADNESS_ASSERT
136 
137 /// Same as MADNESS_ASSERT , but never throws
138 
139 /// Behaves like MADNESS_ASSERT, except when the latter throws madness::MadnessException this aborts
140 /// \param[in] condition The condition to be asserted.
141 #define MADNESS_ASSERT_NOEXCEPT(condition)
142 #undef MADNESS_ASSERT_NOEXCEPT
143 
144 #ifdef MADNESS_ASSERTIONS_ABORT
145 # define MADNESS_ASSERT(condition) \
146  do {if (!(condition)) { std::abort(); }} while (0)
147 # define MADNESS_ASSERT_NOEXCEPT(condition) MADNESS_ASSERT(condition)
148 #endif
149 
150 #ifdef MADNESS_ASSERTIONS_DISABLE
151 // this avoid unused variable warnings, see http://cnicholson.net/2009/02/stupid-c-tricks-adventures-in-assert/
152 # define MADNESS_ASSERT(condition) do { (void)sizeof(condition);} while (0)
153 # define MADNESS_ASSERT_NOEXCEPT(condition) MADNESS_ASSERT(condition)
154 #endif
155 
156 #ifdef MADNESS_ASSERTIONS_ASSERT
157 # define MADNESS_ASSERT(condition) assert(condition)
158 # define MADNESS_ASSERT_NOEXCEPT(condition) MADNESS_ASSERT(condition)
159 #endif
160 
161 #ifdef MADNESS_ASSERTIONS_THROW
162 # define MADNESS_ASSERT(condition) \
163  do { \
164  if (!(condition)) { \
165  madness::exception_break(MADNESS_DISPLAY_EXCEPTION_BREAK_MESSAGE); \
166  throw madness::MadnessException("MADNESS ASSERTION FAILED: " , \
167  (#condition),0,__LINE__,__FUNCTION__,__FILE__); \
168  } \
169  } while (0)
170 # define MADNESS_ASSERT_NOEXCEPT(condition) \
171  do {if (!(condition)) { std::abort(); }} while (0)
172 #endif
173 
174 // the following define/undef are for documentation purposes only.
175 /// Check a condition --- even in a release build the condition is always evaluated so it can have side effects
176 
177 /// Depending on the configuration, one of the following happens if
178 /// \c condition is false:
179 /// - a \c madness::MadnessException is thrown.
180 /// - execution is aborted.
181 /// \param[in] condition The condition to be checked.
182 #define MADNESS_CHECK(condition)
183 #undef MADNESS_CHECK
184 
185 // If madness assertions throw/assert/disabled, then madness checks throw. Otherwise both assertions and checks abort.
186 #ifdef MADNESS_ASSERTIONS_ABORT
187 # define MADNESS_CHECK(condition) \
188  do {if (!(condition)) { std::abort(); }} while (0)
189 #else
190 # define MADNESS_CHECK(condition) \
191  do { \
192  if (!(condition)) { \
193  madness::exception_break(MADNESS_DISPLAY_EXCEPTION_BREAK_MESSAGE); \
194  throw madness::MadnessException("MADNESS CHECK FAILED: " , \
195  (#condition),0,__LINE__,__FUNCTION__,__FILE__); \
196  } \
197  } while (0)
198 #endif
199 
200 
201 // the following define/undef are for documentation purposes only.
202 /// Check a condition --- even in a release build the condition is always evaluated so it can have side effects
203 
204 /// will always throw a madness::MadnessException, but will not print to stderr
205 /// \param[in] condition The condition to be checked.
206 /// \param[in] msg The message to be printed
207 #define MADNESS_CHECK_THROW(condition,msg)
208 #undef MADNESS_CHECK_THROW
209 
210 #define MADNESS_CHECK_THROW(condition,msg) \
211  do { \
212  if (!(condition)) { \
213  throw madness::MadnessException(msg, (#condition),0,__LINE__,__FUNCTION__,__FILE__); \
214  } \
215  } while (0)
216 
217 
218 } // namespace madness
219 
220 #endif // MADNESS_WORLD_MADNESS_EXCEPTION_H__INCLUDED
Base class for exceptions thrown in MADNESS.
Definition: madness_exception.h:66
virtual const char * what() const
Returns the error message, as specified by std::exception.
Definition: madness_exception.h:97
const char * function
Function where the exception occurred.
Definition: madness_exception.h:72
MadnessException(const char *m, const char *a, int v, int l, const char *fn, const char *f)
Constructor that processes the requisite information.
Definition: madness_exception.h:85
const char * msg
The error message.
Definition: madness_exception.h:68
const int value
Value associated with the exception.
Definition: madness_exception.h:70
const char * filename
File where the exception occurred.
Definition: madness_exception.h:73
const int line
Line number where the exception occurred.
Definition: madness_exception.h:71
const char * assertion
String describing the assertion.
Definition: madness_exception.h:69
const double m
Definition: gfit.cc:199
static const double v
Definition: hatom_sf_dirac.cc:20
Macros and tools pertaining to the configuration of MADNESS.
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
std::ostream & operator<<(std::ostream &os, const particle< PDIM > &p)
Definition: lowrankfunction.h:397
NDIM & f
Definition: mra.h:2416
void exception_break(bool message)
This function is executed just before a MadnessException is thrown.
Definition: madness_exception.cc:59
static const double a
Definition: nonlinschro.cc:118
void e()
Definition: test_sig.cc:75