Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
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 
78 
79 } // namespace Teuchos
80 
81 
82 #ifdef HAVE_TEUCHOS_STACKTRACE
83 # define TEUCHOS_STORE_STACKTRACE() \
84  if (Teuchos::TestForException_getEnableStacktrace()) { \
85  Teuchos::store_stacktrace(); \
86  }
87 #else
88 # define TEUCHOS_STORE_STACKTRACE()
89 #endif
90 
91 
167 #define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg) \
168 { \
169  const bool throw_exception = (throw_exception_test); \
170  if(throw_exception) { \
171  Teuchos::TestForException_incrThrowNumber(); \
172  std::ostringstream omsg; \
173  omsg \
174  << __FILE__ << ":" << __LINE__ << ":\n\n" \
175  << "Throw number = " << Teuchos::TestForException_getThrowNumber() \
176  << "\n\n" \
177  << "Throw test that evaluated to true: "#throw_exception_test \
178  << "\n\n" \
179  << msg; \
180  const std::string &omsgstr = omsg.str(); \
181  TEUCHOS_STORE_STACKTRACE(); \
182  Teuchos::TestForException_break(omsgstr); \
183  throw Exception(omsgstr); \
184  } \
185 }
186 
187 
229 #define TEUCHOS_TEST_FOR_EXCEPTION_CLASS_FUNC(throw_exception_test, Exception, msg) \
230 { \
231  TEUCHOS_TEST_FOR_EXCEPTION( (throw_exception_test), Exception, \
232  Teuchos::typeName(*this) << "::" << tfecfFuncName << msg ) \
233 }
234 
235 
243 #define TEUCHOS_TEST_FOR_EXCEPTION_PURE_MSG(throw_exception_test, Exception, msg) \
244 { \
245  const bool throw_exception = (throw_exception_test); \
246  if(throw_exception) { \
247  Teuchos::TestForException_incrThrowNumber(); \
248  std::ostringstream omsg; \
249  omsg << msg; \
250  omsg << "\n\nThrow number = " << Teuchos::TestForException_getThrowNumber() << "\n\n"; \
251  const std::string &omsgstr = omsg.str(); \
252  Teuchos::TestForException_break(omsgstr); \
253  TEUCHOS_STORE_STACKTRACE(); \
254  throw Exception(omsgstr); \
255  } \
256 }
257 
258 
276 #define TEUCHOS_TEST_FOR_EXCEPTION_PRINT(throw_exception_test, Exception, msg, out_ptr) \
277 try { \
278  TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg); \
279 } \
280 catch(const std::exception &except) { \
281  std::ostream *l_out_ptr = (out_ptr); \
282  if(l_out_ptr) { \
283  *l_out_ptr \
284  << "\nThrowing an std::exception of type \'"<<Teuchos::typeName(except) \
285  <<"\' with the error message: " \
286  << except.what(); \
287  } \
288  throw; \
289 }
290 
291 
304 #define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test) \
305  TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, std::logic_error, "Error!")
306 
307 
324 #define TEUCHOS_TEST_FOR_EXCEPT_MSG(throw_exception_test, msg) \
325  TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, std::logic_error, msg)
326 
327 
341 #define TEUCHOS_TEST_FOR_EXCEPT_PRINT(throw_exception_test, out_ptr) \
342  TEUCHOS_TEST_FOR_EXCEPTION_PRINT(throw_exception_test, std::logic_error, "Error!", out_ptr)
343 
344 
353 #define TEUCHOS_TRACE(exc)\
354 { \
355  std::ostringstream omsg; \
356  omsg << exc.what() << std::endl \
357  << "caught in " << __FILE__ << ":" << __LINE__ << std::endl ; \
358  throw std::runtime_error(omsg.str()); \
359 }
360 
361 
362 
363 
364 #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.
#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.