home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX1607.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\EX1607I
- // where cd: is the drive of the CD-ROM
- //----------------------------------------------------------
-
- #include <iostream.h>
- #include <string.h>
- #include <stdio.h>
-
- //
- // This example shows how to use inheritance to organize
- // exceptions into a class hierarchy
- //
- typedef char String32[33];
-
- //
- // Exception is the base exception class
- //
- class Exception {
-
- protected:
- String32 sName;
-
- public:
- //
- // Constructor, sets up the name of the exception
- //
- Exception() { strcpy( (char *)sName, "Exception");}
-
- //
- // Name returns the name of the exception
- //
- char*
- Name(){return (char *)sName;}
- };
-
- //
- // MathExcept is derived from exception
- // use to throw math exception
- //
- class MathExcept : public Exception
- {
-
- public:
- //
- // Constructor, sets up the name of the exception
- //
- MathExcept(){ strcpy( (char *)sName, "Math Exception"); }
- };
-
-
- //
- // IOExcept, is derived from class Exception
- // use to throw ioexcept exceptions
- //
- class IOExcept : public Exception
- {
- public:
- //
- // Constructor, sets up the name of the exception
- //
- IOExcept(){ strcpy( (char *)sName, "IO Exception"); }
- };
-
- //
- // BoundsExcept, is derived from class Exception
- // use to throw a Bounds Violation exceptions
- //
- class BoundsExcept : public Exception
- {
- public:
- //
- // Constructor, sets up the name of the exception
- //
- BoundsExcept(){ strcpy( (char *)sName, "Bounds Exception"); }
- };
-
-
- //
- // divide: returns x divided by y
- // throws MathExcept if y == 0
- //
- long divide( long x, long y)
- {
- if (y==0) {
- throw( MathExcept());
- }
- return (x/y);
- }
-
- //
- // read: dummy function
- // throws IOExcept
- //
- void read()
- {
- FILE *fp;
-
- if ( fp = fopen( "Egon", "r"), fp == NULL) {
- throw( IOExcept());
- }
- // read a file
- }
-
- //
- // access returns an element of a char array
- // throw Exception
- //
- char access( unsigned int index)
- {
- char array[] = {'a', 'b', 'c'};
- if (index >= 3) {
- throw( BoundsExcept());
- }
- else
- return array[index];
- }
-
- //
- // The main routine calls three functions, each of which calls
- // an exception
- //
- main()
- {
- for (int i = 1; i <= 3 ; i++)
- {
- try
- {
- if ( i == 1 )
- divide(1,0); // attempt to divide 1 by 0
- else if (i == 2)
- read(); // always throws an IOExcept
- else if (i == 3)
- access( 4); // always throws an Exception
- }
- catch ( MathExcept except)
- {
- cout << "A(n) " << except.Name()
- << " exception was caught by the MathExcept handler."
- << endl;
- }
- catch ( IOExcept except)
- {
- cout << "A(n) " << except.Name()
- << " exception was caught by the IOExcept handler."
- << endl;
- }
- catch ( Exception except)
- {
- cout << "A(n) " << except.Name()
- << " exception was caught by the Exception handler."
- << endl;
- }
- }
- }
-