// ;----------------------------------------------------------- // ; Program Name: Class_Demo // ; File: demoprob16.cpp // ; Description: Demos the matherr function // ; Author: Jim Adams // ; Environment: MS C++ 6.0 // ; Date Written: 10/27/2001 // ;----------------------------------------------------------- #include #include #include double x=0.0; bool math_was_good; void main() { /* Do several math operations that cause errors. The _matherr * routine handles _DOMAIN errors, but lets the system handle * other errors normally. */ math_was_good=true; x=log(-2.0); if (math_was_good) cout << " log(-2.0) = " << x << endl; math_was_good=true; x=log10(-5.0); if (math_was_good) cout << "log10(-5.0) = " << x << endl; math_was_good=true; x=tan(45); if (math_was_good) cout << " tan(45) = " << x << endl; } // Handle several math errors int _matherr(struct _exception *except) { if( except->type == _DOMAIN ) { cout << "Math Error: " << except->name << "(" << except->arg1 << ")" << endl; math_was_good=false; return 1; } else { math_was_good=true; return 0; } }