home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1610.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\EX1610I
- // where cd: is the drive of the CD-ROM
- //----------------------------------------------------------
-
- #include <iostream.h>
- #include <string.h>
-
- //
- // This is example shows the use of exception specifications
- //
-
- typedef char String32[33];
-
- class Exception {
-
- protected:
- String32 sName;
-
- public:
- Exception() { strcpy( (char *)sName, "Exception");}
-
- char*
- Name(){return (char *)sName;}
- };
-
- class MathExcept : public Exception
- {
-
- public:
- MathExcept(){ strcpy( (char *)sName, "Math Exception"); }
- };
-
-
- long divide( long x, long y) throw( Exception)
- {
- if (y==0)
- {
- cout << "Exception Specification: throw( Exception);"
- << endl
- << "Throwing a MathExcept " << endl;
- throw ( MathExcept());
- }
- return (x/y);
- }
-
- main()
- {
- try
- {
- long z = divide(1, 0);
- }
- catch( Exception except)
- {
- cout << "A(n) " << except.Name()
- << " exception was caught."
- << endl;
- }
- }
-