#include using namespace std; #define NDEBUG #include #include class DivideByZeroException { public: DivideByZeroException(){} DivideByZeroException(int& x){} //... }; class CErrorInMyDestructor { //... public: ~CErrorInMyDestructor() { throw 7.5; } }; void TryThrowCatch(); void UnhandeledExceptionInCatch(); void YouCatchWhatYouThrow(); void ThrowingFromAConditionalExpression(); void RedirectTerminate(); void GoodByeWorld(); void AssertExample(); void RethrowingAnException (); void TestExceptionSpecification(); void ProcessingUnexpectedExceptions(); void ExceptionThrownInDestructor(); void Example11(); void Example12(); void Example13(); void ExceptionSpecification() throw(int, DivideByZeroException); //C++ exception specification ignored except to indicate a function is not __declspec(nothrow) //A function is declared using exception specification, which Visual C++ accepts but does not implement. //Code with exception specifications that are ignored during compilation may need to be //recompiled and linked to be reused in future versions supporting exception specifications. //You can avoid this warning by using the warning pragma: #pragma warning( disable : 4290 ) void main() { } void Divide(double Numerator, double Denominator) { if( Denominator == 0 ) { throw DivideByZeroException(); cout << "Hi will not appear" << endl; } else { cout << Numerator << "/" << Denominator << " = " << Numerator/Denominator << endl; } } void TryThrowCatch() { double Numerator; double Denominator; cin >> Numerator; cin >> Denominator; try { Divide(Numerator, Denominator); } catch ( DivideByZeroException Exception ) { cout << Numerator << "/" << Denominator << " = " << "Undefined" << endl; } } void UnhandeledExceptionInCatch() { double Numerator; double Denominator; cin >> Numerator; cin >> Denominator; try { Divide(Numerator, Denominator); } catch ( DivideByZeroException Exception ) { Divide(Numerator, Denominator); } } void YouCatchWhatYouThrow() { try { throw 7.5; } catch ( char Exception ) { cout << "Caught a char." << endl; } catch ( double Exception ) { cout << "Caught a double." << endl; } catch ( int Exception ) { cout << "Caught an int." << endl; } } void ThrowingFromAConditionalExpression() { int x = 7; double y = 7.5; try { //Deitel Quote //Use caution when throwing a value from a conditional expression, //because promotion rules could cause the value to be of a type //different from the one expected. throw (true ? x : y); } catch ( char Exception ) { cout << "Caught a char." << endl; } catch ( double Exception ) { cout << "Caught a double." << endl; } catch ( int Exception ) { cout << "Caught a int." << endl; } } void GoodByeWorld() { cout << "Good Bye World" << endl; } void RedirectTerminate() { //An exception that is not caught calles terminate set_terminate(&GoodByeWorld); throw 7; } void AssertExample() { assert(false); } void RethrowingAnException() { try { try { throw 8; } catch (int Exception) { throw(Exception); } } catch( int Exception) { cout << "I Caught a rethrown exception" << endl; } } void TestExceptionSpecification() { try { ExceptionSpecification(); } catch(int Exception) { cout << "I caught " << Exception << endl; } catch( DivideByZeroException Exception) { cout << "I caught a Divide by 0 exception" << endl; } } void ExceptionSpecification() throw(int, DivideByZeroException) { throw 7.88888; } void ProcessingUnexpectedExceptions() { set_unexpected(&GoodByeWorld); try { ExceptionSpecification(); } catch ( double x ) { cout << "Caught an double " << x << endl; } catch(...) { unexpected(); } } void ExceptionThrownInDestructor() { try { CErrorInMyDestructor ErrorInMyDestructor; throw 5; } catch (int x) { cout << "Caught an int " << x << endl; } }