Teuchos - Trilinos Tools Package Version of the Day
Defines | Functions
Utility code for throwing exceptions and setting breakpoints.
Teuchos Language Support Utilities

Defines

#define TEUCHOS_ASSERT(assertion_test)   TEST_FOR_EXCEPT(!(assertion_test))
 This macro is throws when an assert fails.
#define TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE(index, lower_inclusive, upper_exclusive)
 This macro asserts that an integral number fallis in the range [lower_inclusive,upper_exclusive)
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
 This macro is checks that to numbers are equal and if not then throws an exception with a good error message.
#define TEUCHOS_ASSERT_INEQUALITY(val1, comp, val2)
 This macro is checks that an inequality between two numbers is satisified and if not then throws a good exception message.
#define TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
 Macro for throwing an exception with breakpointing to ease debugging.

Functions

TEUCHOS_LIB_DLL_EXPORT void TestForException_incrThrowNumber ()
 Increment the throw number.
TEUCHOS_LIB_DLL_EXPORT int TestForException_getThrowNumber ()
 Increment the throw number.
TEUCHOS_LIB_DLL_EXPORT void TestForException_break (const std::string &msg)
 The only purpose for this function is to set a breakpoint.

Define Documentation

#define TEUCHOS_ASSERT (   assertion_test)    TEST_FOR_EXCEPT(!(assertion_test))

This macro is throws when an assert fails.

Note:
The std::exception thrown is std::logic_error.
Examples:
FancyOutputting_test.cpp.

Definition at line 42 of file Teuchos_Assert.hpp.

#define TEUCHOS_ASSERT_IN_RANGE_UPPER_EXCLUSIVE (   index,
  lower_inclusive,
  upper_exclusive 
)
Value:
{ \
    TEST_FOR_EXCEPTION( \
      !( (lower_inclusive) <= (index) && (index) < (upper_exclusive) ), \
      std::out_of_range, \
      "Error, the index " #index " = " << (index) << " does not fall in the range" \
      "["<<(lower_inclusive)<<","<<(upper_exclusive)<<")!" ); \
  }

This macro asserts that an integral number fallis in the range [lower_inclusive,upper_exclusive)

Note:
The std::exception thrown is std::out_of_range.

WARNING: This assert will evaluate index, lower_inclusive, and upper_inclusive more than once if there is a failure which will cause the side-effect of an additional evaluation. This is needed because the return types of these values are unknown. Therefore, only pass in arguments that are objects or function calls that have not side-effects!

Definition at line 59 of file Teuchos_Assert.hpp.

#define TEUCHOS_ASSERT_EQUALITY (   val1,
  val2 
)
Value:
{ \
    TEST_FOR_EXCEPTION( \
      (val1) != (val2), std::out_of_range, \
      "Error, (" #val1 " = " << (val1) << ") != (" #val2 " = " << (val2) << ")!" ); \
  }

This macro is checks that to numbers are equal and if not then throws an exception with a good error message.

Note:
The std::exception thrown is std::out_of_range.

WARNING: This assert will evaluate val1 and val2 more than once if there is a failure which will cause the side-effect of an additional evaluation. This is needed because the return types of val1 and val2 are unknown. Therefore, only pass in arguments that are objects or function calls that have not side-effects!

Definition at line 82 of file Teuchos_Assert.hpp.

#define TEUCHOS_ASSERT_INEQUALITY (   val1,
  comp,
  val2 
)
Value:
{ \
    TEST_FOR_EXCEPTION( \
      !( (val1) comp (val2) ), std::out_of_range, \
      "Error, (" #val1 " = " << (val1) << ") " \
      #comp " (" #val2 " = " << (val2) << ")! FAILED!" ); \
  }

This macro is checks that an inequality between two numbers is satisified and if not then throws a good exception message.

Note:
The std::exception thrown is std::out_of_range.

WARNING: This assert will evaluate val1 and val2 more than once if there is a failure which will cause the side-effect of an additional evaluation. This is needed because the return types of val1 and val2 are unknown. Therefore, only pass in arguments that are objects or function calls that have not side-effects!

Definition at line 103 of file Teuchos_Assert.hpp.

#define TEST_FOR_EXCEPTION (   throw_exception_test,
  Exception,
  msg 
)
Value:
{ \
    const bool throw_exception = (throw_exception_test); \
    if(throw_exception) { \
      TestForException_incrThrowNumber(); \
      std::ostringstream omsg; \
      omsg \
        << __FILE__ << ":" << __LINE__ << ":\n\n" \
        << "Throw number = " << TestForException_getThrowNumber() << "\n\n" \
        << "Throw test that evaluated to true: "#throw_exception_test << "\n\n" \
        << msg; \
      const std::string &omsgstr = omsg.str(); \
      TestForException_break(omsgstr); \
      throw Exception(omsgstr); \
    } \
}

Macro for throwing an exception with breakpointing to ease debugging.

Parameters:
throw_exception_test[in] Test for when to throw the exception. This can and should be an expression that may mean something to the user. The text verbatim of this expression is included in the formed error string.
Exception[in] This should be the name of an exception class. The only requirement for this class is that it have a constructor that accepts an std::string object (as all of the standard exception classes do).
msg[in] This is any expression that can be included in an output stream operation. This is useful when buinding error messages on the fly. Note that the code in this argument only gets evaluated if throw_exception_test evaluates to true when an exception is throw.

The way that this macro is intended to be used is to call it in the source code like a function. For example, suppose that in a piece of code in the file my_source_file.cpp that the exception std::out_of_range is thrown if n > 100. To use the macro, the source code would contain (at line 225 for instance):


 TEST_FOR_EXCEPTION( n > 100, std::out_of_range,
    "Error, n = " << n << is bad" );
 

When the program runs and with n = 125 > 100 for instance, the std::out_of_range exception would be thrown with the error message:


 /home/bob/project/src/my_source_file.cpp:225: n > 100: Error, n = 125 is bad
 

In order to debug this, simply open your debugger (gdb for instance), set a break point at my_soure_file.cpp:225 and then set the condition to break for n > 100 (e.g. in gdb the command is cond break_point_number n > 100 and then run the program. The program should stop a the point in the source file right where the exception will be thrown at but before the exception is thrown. Try not to use expression for throw_exception_test that includes virtual function calls, etc. as most debuggers will not be able to check these types of conditions in order to stop at a breakpoint. For example, instead of:


 TEST_FOR_EXCEPTION( obj1->val() > obj2->val(), std::logic_error, "Oh no!" );
 

try:


 double obj1_val = obj1->val(), obj2_val = obj2->val();
 TEST_FOR_EXCEPTION( obj1_val > obj2_val, std::logic_error, "Oh no!" );
 

If the developer goes to the line in the source file that is contained in the error message of the exception thrown, he/she will see the underlying condition.

As an alternative, you can set a breakpoint for any exception thrown by setting a breakpoint in the function ThrowException_break().

NOTE: This macro will only evaluate throw_exception_test once reguardless if the test fails and the exception is thrown or not. Therefore, it is safe to call a function with side-effects as the throw_exception_test argument.

Definition at line 122 of file Teuchos_TestForException.hpp.


Function Documentation

TEUCHOS_LIB_DLL_EXPORT void TestForException_incrThrowNumber ( )

Increment the throw number.

Definition at line 35 of file Teuchos_TestForException.cpp.

TEUCHOS_LIB_DLL_EXPORT int TestForException_getThrowNumber ( )

Increment the throw number.

Definition at line 41 of file Teuchos_TestForException.cpp.

TEUCHOS_LIB_DLL_EXPORT void TestForException_break ( const std::string &  msg)

The only purpose for this function is to set a breakpoint.

Definition at line 47 of file Teuchos_TestForException.cpp.

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines