Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_TestForException.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_TEST_FOR_EXCEPTION_H
43 #define TEUCHOS_TEST_FOR_EXCEPTION_H
44 
50 #include "Teuchos_stacktrace.hpp"
51 
52 
53 namespace Teuchos {
54 
55 
62 
65 
68 TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_break( const std::string &msg );
69 
73 
77 
79 TEUCHOSCORE_LIB_DLL_EXPORT [[noreturn]] void TestForTermination_terminate(const std::string &msg);
80 
81 
82 } // namespace Teuchos
83 
84 
85 #ifdef HAVE_TEUCHOS_STACKTRACE
86 # define TEUCHOS_STORE_STACKTRACE() \
87  if (Teuchos::TestForException_getEnableStacktrace()) { \
88  Teuchos::store_stacktrace(); \
89  }
90 #else
91 # define TEUCHOS_STORE_STACKTRACE()
92 #endif
93 
94 
170 #define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg) \
171 { \
172  const bool throw_exception = (throw_exception_test); \
173  if(throw_exception) { \
174  Teuchos::TestForException_incrThrowNumber(); \
175  std::ostringstream omsg; \
176  omsg \
177  << __FILE__ << ":" << __LINE__ << ":\n\n" \
178  << "Throw number = " << Teuchos::TestForException_getThrowNumber() \
179  << "\n\n" \
180  << "Throw test that evaluated to true: "#throw_exception_test \
181  << "\n\n" \
182  << msg; \
183  const std::string &omsgstr = omsg.str(); \
184  TEUCHOS_STORE_STACKTRACE(); \
185  Teuchos::TestForException_break(omsgstr); \
186  throw Exception(omsgstr); \
187  } \
188 }
189 
190 
232 #define TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(throw_exception_test, Exception, msg) \
233 { \
234  TEUCHOS_TEST_FOR_EXCEPTION( (throw_exception_test), Exception, \
235  Teuchos::typeName(*this) << "::" << tfecfFuncName << msg ) \
236 }
237 
238 
246 #define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg) \
247 { \
248  const bool throw_exception = (throw_exception_test); \
249  if(throw_exception) { \
250  Teuchos::TestForException_incrThrowNumber(); \
251  std::ostringstream omsg; \
252  omsg << msg; \
253  omsg << "\n\nThrow number = " << Teuchos::TestForException_getThrowNumber() << "\n\n"; \
254  const std::string &omsgstr = omsg.str(); \
255  Teuchos::TestForException_break(omsgstr); \
256  TEUCHOS_STORE_STACKTRACE(); \
257  throw Exception(omsgstr); \
258  } \
259 }
260 
261 
279 #define TEUCHOS_TEST_FOR_EXCEPTION_PRINT(throw_exception_test, Exception, msg, out_ptr) \
280 try { \
281  TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg); \
282 } \
283 catch(const std::exception &except) { \
284  std::ostream *l_out_ptr = (out_ptr); \
285  if(l_out_ptr) { \
286  *l_out_ptr \
287  << "\nThrowing an std::exception of type \'"<<Teuchos::typeName(except) \
288  <<"\' with the error message: " \
289  << except.what(); \
290  } \
291  throw; \
292 }
293 
294 
307 #define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test) \
308  TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, std::logic_error, "Error!")
309 
310 
327 #define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg) \
328  TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, std::logic_error, msg)
329 
330 
344 #define TEUCHOS_TEST_FOR_EXCEPT_PRINT(throw_exception_test, out_ptr) \
345  TEUCHOS_TEST_FOR_EXCEPTION_PRINT(throw_exception_test, std::logic_error, "Error!", out_ptr)
346 
347 
356 #define TEUCHOS_TRACE(exc)\
357 { \
358  std::ostringstream omsg; \
359  omsg << exc.what() << std::endl \
360  << "caught in " << __FILE__ << ":" << __LINE__ << std::endl ; \
361  throw std::runtime_error(omsg.str()); \
362 }
363 
379 #define TEUCHOS_TEST_FOR_TERMINATION(terminate_test, msg) \
380 { \
381  const bool call_terminate = (terminate_test); \
382  if (call_terminate) { \
383  std::ostringstream omsg; \
384  omsg \
385  << __FILE__ << ":" << __LINE__ << ":\n\n" \
386  << "Terminate test that evaluated to true: "#terminate_test \
387  << "\n\n" \
388  << msg << "\n\n"; \
389  auto str = omsg.str(); \
390  Teuchos::TestForTermination_terminate(str); \
391  } \
392 }
393 
394 #endif // TEUCHOS_TEST_FOR_EXCEPTION_H
Functions for returning stacktrace info (GCC only initially).
TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_setEnableStacktrace(bool enableStrackTrace)
Set at runtime if stacktracing functionality is enabled when * exceptions are thrown.
TEUCHOSCORE_LIB_DLL_EXPORT[[noreturn]] void TestForTermination_terminate(const std::string &msg)
Prints the message to std::cerr and calls std::terminate.
#define TEUCHOSCORE_LIB_DLL_EXPORT
TEUCHOSCORE_LIB_DLL_EXPORT int TestForException_getThrowNumber()
Increment the throw number.
TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_incrThrowNumber()
Increment the throw number.
TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_break(const std::string &msg)
The only purpose for this function is to set a breakpoint.
Defines basic traits returning the name of a type in a portable and readable way. ...
TEUCHOSCORE_LIB_DLL_EXPORT bool TestForException_getEnableStacktrace()
Get at runtime if stacktracing functionality is enabled when exceptions are thrown.