// main() with try/catch for exceptions, calling down to xmain() // (C) Copyright Beman Dawes 1995. Permission to copy, use, modify, sell and // distribute this software is granted provided this copyright notice appears // in all copies. This software is provided "as is" without express or implied // warranty, and with no claim as to its suitability for any purpose. // This header provides a "main" function which catches exceptions and reports // them as errors to cerr. // You must provide a function "xmain" which is otherwise just like "main". #include int xmain( int argc, c_str * argv[] ); // prototype for your program int main( int argc, c_str * argv[] ) { int result = 0; // = 0 needed to quiet VC++ 4.2 warning C4701 bool excpt = false; try { result = xmain( argc, argv ); } // detail error reporting goes to cout so it is interlaced with other output catch ( std::runtime_error & ex ) { cout << "\n**** runtime_error exception: " << ex.what() << endl; excpt = true; } catch ( ... ) { cout << "\n**** unknown exception" << endl; excpt = true; } if ( excpt ) { // overall message goes to cerr in case cout is redirected cerr << "********** PROGRAM CANCELLED BEFORE WORK COMPLETED ***********\n" << "*** You must take special action to deal with this error. ***\n" << "*** Ask for help if you are unsure of what action to take. ***" << endl; result = EXIT_FAILURE; } return result; } // main