Chapter Contents

Previous

Next
Handling Exceptions in SAS/C

Exception Tracebacks

Because of unwinding, information about the context of a throw will no longer be available at the point when the exception is handled or the exception handling mechanism decides that the exception has no handler. In order to simplify problem diagnosis, traceback information is normally captured when an exception is thrown. The information remains available throughout the lifetime of the exception. This information can be displayed with the =xtrace run-time option or the function std::exception::xtrace.

If QUIET is in effect (via an active quiet(1) which was not ignored due to the =warning run-time option) and it is not overridden by =xtrace, then traceback information will not be collected at throws. This allows the collection overhead to be avoided. Note that the traceback information may also not be collected if insufficient memory is available.

The function std::exception::xtrace outputs the collected traceback information, if any, for the currently handled exception. This is the exception for the catch handler that has been most recently entered but not exited. This is the same exception that would be rethrown by a throw; statement. The function is similar to the C library function btrace. It takes a function pointer to redirect output. When the pointer is 0, the output goes to the stderr stream unless the SPE library is being used, in which case the call is ignored.

Exception traceback information may be output due to the =xtrace run-time option. If =xtrace is specified then the collected traceback is output immediately to the stderr stream, preceded by run-time message LSCX251. The message includes the type of the exception object, if its type is one of the standard library exception classes. The std::exception::is_xtraced function is provided to determine if the traceback information for the current exception has been previously output. This allows duplicate output to be avoided. With the SPE library, no tracebacks will be printed except for explicit calls to std::exception::xtrace that specify a non-NULL argument.

The following code example demonstrates one use of xtrace and is_xtraced. This will print to stderr a traceback from the point at which the library operator new function throws the std::bad_alloc exception.

#include     #include 
void throwit()
{
   // this allocation is expected to be unlikely to succeed
   // this should throw std::bad_alloc
   char* p = new char[256*1024*1024];
   delete [] p;
}

int main()
{
   try
   {
      throwit();
   }

   catch ( const std::exception& e )
   {
      if ( !std::exception::is_xtraced() )
      {
         cerr << "xtrace() information follows for class "
              << e.what()
              << endl;
         std::exception::xtrace();
      }
   }
}


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.