Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
48
51
52
53namespace Teuchos {
54
55
59
61TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_incrThrowNumber();
62
64TEUCHOSCORE_LIB_DLL_EXPORT int TestForException_getThrowNumber();
65
68TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_break(const std::string &msg,
69 int throwNumber);
70
73TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_setEnableStacktrace(bool enableStrackTrace);
74
77TEUCHOSCORE_LIB_DLL_EXPORT bool TestForException_getEnableStacktrace();
78
80[[noreturn]] TEUCHOSCORE_LIB_DLL_EXPORT void TestForTermination_terminate(const std::string &msg);
81
82
83} // namespace Teuchos
84
85
86#ifdef HAVE_TEUCHOS_STACKTRACE
87# define TEUCHOS_STORE_STACKTRACE() \
88 if (Teuchos::TestForException_getEnableStacktrace()) { \
89 Teuchos::store_stacktrace(); \
90 }
91#else
92# define TEUCHOS_STORE_STACKTRACE()
93#endif
94
95
178#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg) \
179{ \
180 const bool throw_exception = (throw_exception_test); \
181 if(throw_exception) { \
182 Teuchos::TestForException_incrThrowNumber(); \
183 const int throwNumber = Teuchos::TestForException_getThrowNumber(); \
184 std::ostringstream omsg; \
185 omsg \
186 << __FILE__ << ":" << __LINE__ << ":\n\n" \
187 << "Throw number = " << throwNumber \
188 << "\n\n" \
189 << "Throw test that evaluated to true: "#throw_exception_test \
190 << "\n\n" \
191 << msg; \
192 const std::string &omsgstr = omsg.str(); \
193 TEUCHOS_STORE_STACKTRACE(); \
194 Teuchos::TestForException_break(omsgstr, throwNumber); \
195 throw Exception(omsgstr); \
196 } \
197}
198
199
241#define TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(throw_exception_test, Exception, msg) \
242{ \
243 TEUCHOS_TEST_FOR_EXCEPTION( (throw_exception_test), Exception, \
244 Teuchos::typeName(*this) << "::" << tfecfFuncName << msg ) \
245}
246
247
255#define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg) \
256{ \
257 const bool throw_exception = (throw_exception_test); \
258 if(throw_exception) { \
259 Teuchos::TestForException_incrThrowNumber(); \
260 const int throwNumber = Teuchos::TestForException_getThrowNumber(); \
261 std::ostringstream omsg; \
262 omsg << msg; \
263 omsg << "\n\nThrow number = " << throwNumber << "\n\n"; \
264 const std::string &omsgstr = omsg.str(); \
265 Teuchos::TestForException_break(omsgstr, throwNumber); \
266 TEUCHOS_STORE_STACKTRACE(); \
267 throw Exception(omsgstr); \
268 } \
269}
270
271
289#define TEUCHOS_TEST_FOR_EXCEPTION_PRINT(throw_exception_test, Exception, msg, out_ptr) \
290try { \
291 TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg); \
292} \
293catch(const std::exception &except) { \
294 std::ostream *l_out_ptr = (out_ptr); \
295 if(l_out_ptr) { \
296 *l_out_ptr \
297 << "\nThrowing an std::exception of type \'"<<Teuchos::typeName(except) \
298 <<"\' with the error message: " \
299 << except.what(); \
300 } \
301 throw; \
302}
303
304
317#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test) \
318 TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, std::logic_error, "Error!")
319
320
337#define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg) \
338 TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, std::logic_error, msg)
339
340
354#define TEUCHOS_TEST_FOR_EXCEPT_PRINT(throw_exception_test, out_ptr) \
355 TEUCHOS_TEST_FOR_EXCEPTION_PRINT(throw_exception_test, std::logic_error, "Error!", out_ptr)
356
357
366#define TEUCHOS_TRACE(exc)\
367{ \
368 std::ostringstream omsg; \
369 omsg << exc.what() << std::endl \
370 << "caught in " << __FILE__ << ":" << __LINE__ << std::endl ; \
371 throw std::runtime_error(omsg.str()); \
372}
373
389#define TEUCHOS_TEST_FOR_TERMINATION(terminate_test, msg) \
390{ \
391 const bool call_terminate = (terminate_test); \
392 if (call_terminate) { \
393 std::ostringstream omsg; \
394 omsg \
395 << __FILE__ << ":" << __LINE__ << ":\n\n" \
396 << "Terminate test that evaluated to true: "#terminate_test \
397 << "\n\n" \
398 << msg << "\n\n"; \
399 auto str = omsg.str(); \
400 Teuchos::TestForTermination_terminate(str); \
401 } \
402}
403
404#endif // TEUCHOS_TEST_FOR_EXCEPTION_H
Defines basic traits returning the name of a type in a portable and readable way.
Functions for returning stacktrace info (GCC only initially).
TEUCHOSCORE_LIB_DLL_EXPORT int TestForException_getThrowNumber()
Increment the throw number.
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 TestForException_break(const std::string &msg, int throwNumber)
The only purpose for this function is to set a breakpoint.
TEUCHOSCORE_LIB_DLL_EXPORT void TestForException_incrThrowNumber()
Increment the throw number.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOSCORE_LIB_DLL_EXPORT bool TestForException_getEnableStacktrace()
Get 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.