/*************************************************************************
File: unexpect.cpp
Description: This file contains an example of handling unexpected exceptions.
   A function throwing an undefined exception will cause an unexpected
   excpetion to occur and terminate unless we override that behavior.
Output: Cool, caught=1
	Cool, caught=2
	unexpected error!
	Cool, caught=3
	>>>Program Aborted<<<
Source(s): "More Effective C+", Scott Meyers
Course Section: Advanced C++ Errors and Exceptions
Author: j. stafford
Date: 970127
*************************************************************************/
#include <iostream.h>
#include <except.h>
#define LOGERROR { cerr << "Error, line:" << __LINE__ << endl; error++; }
#define COOL     { cout << "Cool, caught=" << caught << endl; }
void throwsException() throw(int) { throw -1; }
void couldButDoesNot()            { }
void couldAndDoes()               { throwsException(); }
void shouldntButDoes() throw()    { throwsException(); }
class UnexpectedException {};
void unexpectedHandler() {
   cerr << "unexpected error!" << endl;
   throw UnexpectedException();
}
main() {
   int caught=0, error=0;

      //call and catch an advertised int exception
   try {
      throwsException();
   } catch (...) { caught+=1;  }
   if (caught != 1) LOGERROR
   else COOL

      //call and catch an undetermined exception
   try {
      couldAndDoes();
   } catch(...) { caught += 1; }
   if (caught != 2) LOGERROR
   else COOL

      //setup to handle an unexpect exception
   void (*handler)() = set_unexpected(unexpectedHandler);
      //call someone that shouldn't throw exdeption, but does anyway
   try {
      shouldntButDoes();
   } catch(...) { caught += 1; }
   if (caught != 3) LOGERROR
   else COOL

      //put back the default handler
   set_unexpected(handler);
      //now try that again and die!!!
   try {
      shouldntButDoes();      //this is the last place we exececute
   } catch(...) { caught += 1; }
   if (caught != 4) LOGERROR
   else COOL
   cout << "bye" << endl;     //never gets here
}
//end of file

