home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1609.CPP
-
- //----------------------------------------------------------
- // Exception handling is supported only by
- // the IBM C Set++ Compiler
- // This example does not work in Borland Turbo C++
- // or in Microsoft Visual C++
- //----------------------------------------------------------
- // To run this program on an OS/2 machine:
- // move to an OS/2 window
- // [type] cd:\EXAMPLES\EX1609I
- // where cd: is the drive of the CD-ROM
- //----------------------------------------------------------
-
- #include <iostream.h>
- #include <terminat.h>
- #include <unexpect.h>
-
- //
- // This is example illustrates set_terminate
- //
-
- typedef void(*PFV)();
- PFV pfvOldTerm;
-
-
- //
- // FUNCTION: divide(...) divides first parameter by the second
- // PARAMETERS: long x dividend
- // long y divisor
- // RETURN VALUE: x divided by y
- //
- long divide(long x, long y)
- {
- if (y==0)
- {
- cout << "Divide by zero error, throwing exception."
- << endl;
- throw y; // can't divide by zero so throw an exception
- }
- return x/y; // return the quotient
- }
-
-
- //
- // This fucntion is used to throw an exception
- //
- void Process()
- {
- try
- {
- cout << "Forcing a divide by zero error." << endl;
- long z = divide( 1, 0);
- }
- catch( char)
- {
- cout << "This line should not be printed." << endl;
- }
- }
-
- //
- // This is the new terminate function
- //
- void myTerminate()
- {
- // Reset terminate function
- set_terminate( (PFV)pfvOldTerm);
-
- cout << "The terminate function has been reset"
- << " to the previous state."
- << endl;
-
- cout << "Calling Process() from "
- << "the myTerminate()." << endl;
- Process();
- }
-
-
- main()
- {
- // set new terminate function
- // store old terminate function
- pfvOldTerm = set_terminate( (PFV)myTerminate);
-
- cout << "Setting the terminate function to myTerminate()."
- << endl;
-
- // Insert code here to free up resources
-
- cout << "Calling Process()." << endl;
-
- Process();
- }
-