//============================================================================== // Exception handling demonstration. // Alice E. Fischer, August 2009 except.cpp // Modified by Michael Fischer, November 2010 //============================================================================== // NOTE: Commented-out lines involving "success" are for the purpose of // cleaning up when the Card(istream&) constructor throws an exception. // This is needed by older implementations of g++ but not by g++ 4.4.4. //#include "tools.hpp" #include #include "cards.hpp" #include "bad.hpp" #define NCARDS 3 using namespace std; int main( void ) { Card* hand[NCARDS]; int k; // bool success; Card::instructions( cout, NCARDS ); //------------------------------------------ Main loop that reads all cards. for (k=0; kprint(cout); ++k; } //----------------- Check for the three application-specific exceptions. catch (Bad& bs) { bs.print(); } // Will catch all 3 Bad errors. //------------------- Now check for general exceptions thrown by system. catch (bad_alloc bs) { //-------------- Catch a malloc failure. cerr << " Allocation error for card #" <print( cout ); } // Clean up for (k = 0; k < NCARDS; ++k) { delete hand[k]; } }