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 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_TEST_FOR_EXCEPTION_H
11 #define TEUCHOS_TEST_FOR_EXCEPTION_H
12 
18 #include "Teuchos_stacktrace.hpp"
19 
20 
21 namespace Teuchos {
22 
23 
30 
33 
36 TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_break(const std::string &msg,
37  int throwNumber);
38 
42 
46 
48 [[noreturn]] TEUCHOSCORE_LIB_DLL_EXPORT void TestForTermination_terminate(const std::string &msg);
49 
50 
51 } // namespace Teuchos
52 
53 
54 #ifdef HAVE_TEUCHOS_STACKTRACE
55 # define TEUCHOS_STORE_STACKTRACE() \
56  if (Teuchos::TestForException_getEnableStacktrace()) { \
57  Teuchos::store_stacktrace(); \
58  }
59 #else
60 # define TEUCHOS_STORE_STACKTRACE()
61 #endif
62 
63 
146 #define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg) \
147 { \
148  const bool throw_exception = (throw_exception_test); \
149  if(throw_exception) { \
150  Teuchos::TestForException_incrThrowNumber(); \
151  const int throwNumber = Teuchos::TestForException_getThrowNumber(); \
152  std::ostringstream omsg; \
153  omsg \
154  << __FILE__ << ":" << __LINE__ << ":\n\n" \
155  << "Throw number = " << throwNumber \
156  << "\n\n" \
157  << "Throw test that evaluated to true: "#throw_exception_test \
158  << "\n\n" \
159  << msg; \
160  const std::string &omsgstr = omsg.str(); \
161  TEUCHOS_STORE_STACKTRACE(); \
162  Teuchos::TestForException_break(omsgstr, throwNumber); \
163  throw Exception(omsgstr); \
164  } \
165 }
166 
167 
209 #define TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(throw_exception_test, Exception, msg) \
210 { \
211  TEUCHOS_TEST_FOR_EXCEPTION( (throw_exception_test), Exception, \
212  Teuchos::typeName(*this) << "::" << tfecfFuncName << msg ) \
213 }
214 
215 
223 #define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg) \
224 { \
225  const bool throw_exception = (throw_exception_test); \
226  if(throw_exception) { \
227  Teuchos::TestForException_incrThrowNumber(); \
228  const int throwNumber = Teuchos::TestForException_getThrowNumber(); \
229  std::ostringstream omsg; \
230  omsg << msg; \
231  omsg << "\n\nThrow number = " << throwNumber << "\n\n"; \
232  const std::string &omsgstr = omsg.str(); \
233  Teuchos::TestForException_break(omsgstr, throwNumber); \
234  TEUCHOS_STORE_STACKTRACE(); \
235  throw Exception(omsgstr); \
236  } \
237 }
238 
239 
257 #define TEUCHOS_TEST_FOR_EXCEPTION_PRINT(throw_exception_test, Exception, msg, out_ptr) \
258 try { \
259  TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg); \
260 } \
261 catch(const std::exception &except) { \
262  std::ostream *l_out_ptr = (out_ptr); \
263  if(l_out_ptr) { \
264  *l_out_ptr \
265  << "\nThrowing an std::exception of type \'"<<Teuchos::typeName(except) \
266  <<"\' with the error message: " \
267  << except.what(); \
268  } \
269  throw; \
270 }
271 
272 
285 #define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test) \
286  TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, std::logic_error, "Error!")
287 
288 
305 #define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg) \
306  TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, std::logic_error, msg)
307 
308 
322 #define TEUCHOS_TEST_FOR_EXCEPT_PRINT(throw_exception_test, out_ptr) \
323  TEUCHOS_TEST_FOR_EXCEPTION_PRINT(throw_exception_test, std::logic_error, "Error!", out_ptr)
324 
325 
334 #define TEUCHOS_TRACE(exc)\
335 { \
336  std::ostringstream omsg; \
337  omsg << exc.what() << std::endl \
338  << "caught in " << __FILE__ << ":" << __LINE__ << std::endl ; \
339  throw std::runtime_error(omsg.str()); \
340 }
341 
357 #define TEUCHOS_TEST_FOR_TERMINATION(terminate_test, msg) \
358 { \
359  const bool call_terminate = (terminate_test); \
360  if (call_terminate) { \
361  std::ostringstream omsg; \
362  omsg \
363  << __FILE__ << ":" << __LINE__ << ":\n\n" \
364  << "Terminate test that evaluated to true: "#terminate_test \
365  << "\n\n" \
366  << msg << "\n\n"; \
367  auto str = omsg.str(); \
368  Teuchos::TestForTermination_terminate(str); \
369  } \
370 }
371 
372 #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 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, int throwNumber)
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.