#include #include #include using namespace std; #ifdef GNU_EXCEPTION_BUG #include "exceptions.h" #endif double mysqrt( double x ) throw( int ) { if( x < 0 ) throw 15 ; else return sqrt( x ); } double myexp( double x ) throw( double ) { double result = exp( x ); // call routines in cmath if( result == HUGE_VAL ) throw 5.5 ; else return result; } double f( double x ) { try { return mysqrt( x ) + myexp( x ); } catch( int e ) { cout << "Caught exception " << e << endl; // invoke correct what return -1.0; // can’t be answer for any other x } catch( double e ) { cout << "Caught exception " << e << endl; // invoke correct what return -1.0; // can’t be answer for any other x } } int main( ) { cout << f( 10 ) << endl; cout << f( -4 ) << endl; // illegal sqrt cout << f( 10000000 ) << endl; // illegal exp return 0; }