///////////////////////////////////////////////////////////////
//  except.cpp  -  demonstrate exceptions                    //
//                                                           //
//  Jim Fawcett, modified 3/20/2000                          //
///////////////////////////////////////////////////////////////

#include <iostream>
#include <fstream>

using namespace std;

void main(int argc, char *argv[]) {

  cout << "\n  Demonstrating Exceptions "
       << "\n ==========================\n\n";
  int i;
  cout << "command line arguments: ";
  for(i=1; i<argc; i++)
    cout << argv[i] << ", ";
  cout << endl << endl;
  //////////////////////////////////////////////////////////
  //  region for throwing command line argument exception
  //////////////////////////////////////////////////////////
  try {
    if(argc == 1)
      throw "  please enter at least one file name on command line\n";

    for(i=1; i<argc; i++) {
      //////////////////////////////////////////////////
      //  region for throwing file opening exceptions
      //////////////////////////////////////////////////
      try {
        ifstream in(argv[i]);
        if(!in.good())
          throw "  can't open file ";
        cout << "\n  opening file " << argv[i] << endl;
        int j;
        for(j=0; j<5; j++) {
          char buffer[76];
          in.getline(buffer,75);
          cout << buffer << endl;
        }
        cout << endl;
      }
      //////////////////////////////////////////////
      // error handler for file opening exception
      //////////////////////////////////////////////
      catch(char *msg) {
        cout << msg << argv[i] << "\n\n";
        if(argc == 2)
          throw "  rethrown exception in opening handler";
      }
    }
  }
  /////////////////////////////
  // top level error handler
  /////////////////////////////
  catch(char *msg) {
    cout << msg << endl;
  }
}
  Demonstrating Exceptions 
 ==========================

command line arguments: except.cpp, foobar, 


  opening file except.cpp
///////////////////////////////////////////////////////////////
//  except.cpp  -  demonstrate exceptions                    //
//                                                           //
//  Jim Fawcett, modified 3/20/2000                          //
///////////////////////////////////////////////////////////////

  can't open file foobar

