Inheritance and Exception Handling

In Chapter 9 you saw how to use custom exception classes to enhance your error handling. Exception classes are not too different from any other classes. You will probably have some methods that are common for all exception classes you create. For this reason, it makes sense to create a base class that contains the common elements that you wish all exception classes to have. You can then create specific classes for specific exception types and use them as needed.


Some other programming languages such as Java include a host of such exception classes for you. With C++, you will need to create your own exception classes.

The following example illustrates the use of a base exception class and several derived classes. It is strongly suggested that you consider including the header file with these classes in all your programs so that you might use these exception classes in all the code you write.

Example 11.4

Step 1: Please enter the following code into your favorite text editor and save it in a file name error.h.

#include <fstream> #include <memory> #include <iostream> using namespace std;   class error { protected:  fstream errfile; public:  void printmsg(char *msg);  void logmsg(char *msg); }; void error::printmsg(char *msg) {  cout << msg; } void error::logmsg(char *msg) {"error.log",ios::app);  errfile << msg; } class fileerror:public error    {    };    class divisionbyzero:public error    {    };

Step 2: Enter the following code into your favorite text editor and save it in a file named 11-4.cpp.

#include "error.h" #include <iostream> using namespace std; fileerror fe; divisionbyzero de; int main() {      fstream f;      float dividend, divisor,quotient; try {  cout << "Enter a number " << endl;      cin >> dividend;      cout << "Enter another number " << endl;      cin >> divisor;      // if the divisor is zero we should      // get the divisionbyzero      if(divisor==0)      throw de;      quotient = dividend/divisor;      cout << quotient;      // if there is no file then we should get      // the fileerror"somefile.txt",ios::in);      if(!f)      throw fe; } catch(divisionbyzero d) { d.printmsg("Division by zero \n"); } catch(fileerror f) { f.printmsg("File not found error \n"); } catch(...) { cout<< "Unknown error! \n"; }      return 0;    }

Step 3: Compile your code.

Step 4: Run the program. You should see something like what is depicted in Figure 11.4.

click to expand
Figure 11.4: Custom exception classes.

This example, hopefully, accomplishes several goals. To begin with it illustrates how you might create custom error classes and use inheritance to avoid unnecessarily repeating code. But it is also one more opportunity for you to practice inheritance techniques in C++. You can easily create any type of custom exception class you wish. You can also create the proper types of error functions to do anything you might wish to do to either log the error, or, perhaps, fix the error. You may be wondering how you go about fixing an error. The following example illustrates how you might fix the division by 0 error. The following code snippet adds a function to the division by 0 class, and this function takes the address of the dividend, and changes it to a non-0 number.

void fixzeroerror(float &dividend) {  dividend += 1; }

Now, with this function added to the division by 0 class, all you need to do when a division by 0 error occurs is call this function. This function will change the dividend to a non-0 number.

C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom © 2008-2017.
If you may any questions please contact us: