Chapter Contents |
Previous |
Next |
Handling Exceptions in SAS/C |
#include #include #include #include // a simple user defined class for exception reporting class fpe_error : public std::exception { public: virtual const char* what() const throw() { return "fpe_error"; } }; jmp_buf jb; // trivial signal handler void handler( int signum ) { longjmp( jb, signum ); } // dummy example code, this always generates a SIGFPE. void doit() { raise( SIGFPE ); } // This function "adapts" signals to exceptions. // Since this function calls setjmp(), it should not have any try blocks // or autos with nontrivial destructors. void trapper() { signal( SIGFPE, handler ); if ( setjmp( jb ) ) throw fpe_error(); else { doit(); signal( SIGFPE, SIG_DFL ); } } int main() { try { trapper(); } catch ( const std::exception& e ) { cout << "Caught exception class " << e.what() << endl; } }
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.