home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1613.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\EX1613I
- // where cd: is the drive of the CD-ROM
- //----------------------------------------------------------
-
- #include <iostream.h>
- #include <unexpect.h>
-
- //
- // This is example illustrates the use of set_unexpected
- //
- typedef void (*PFV)(); // Pointer to function that returns void
- PFV pfvOldUnexpected; // use to store old unexpected function.
-
- //
- // 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) throw( long)
- {
- if (y==0)
- {
- throw y; // can't divide by zero so throw an exception
- }
- return x/y; // return the quotient
- }
-
-
- void Process() throw()
- {
- try
- {
- divide( 1, 0);
- }
- catch( char) // char exception handler won't catch a float
- {
- cout << "This line should not be printed." << endl;
- }
- }
-
- //
- // This is the user defined unexpected exception handler
- //
- void myUnexpected()
- {
- cout << "Invoked myUnexpected()." << endl;
-
- set_unexpected( (PFV)pfvOldUnexpected);
- cout << "Reset unexpected function." << endl;
-
- // Insert code to free resources here
-
- cout << "Calling Process() from myUnexpected()" << endl;
- Process();
- }
-
-
- main()
- {
- // set the unexpected function to myUnexpected
- pfvOldUnexpected = set_unexpected( (PFV)myUnexpected);
-
- cout << "Set the unexpected function to myUnexpected()."
- << endl;
-
- Process(); // Call the function to do the work.
- }
-