Classes and Exception Handling


Classes are quite useful and are the cornerstone for C++ programming. One way that many programmers utilize classes is by creating their own specialized classes to handle various exceptions. To do this, a programmer simply creates a class that has the appropriate methods to handle a particular error type. If you think back to Chapter 6, on error handling, you can throw various types of exceptions depending on the situation. This can be useful, particularly if you place these exception classes into a header file, then you can then use them in any program you wish. The following example shows the use of specialized exception classes.

Example 10.4

Step 1: Enter the following code into your favorite text editor and save it as customexception.h.

#include <fstream> #include <iostream> using namespace std;   class divisionbyzero {      fstream myfile; public:          void errormessage();       void logerror(); }; class filenotfound {      fstream myfile; public:      void errormessage();      void logerror(); }; void divisionbyzero::errormessage() { cout << "Division by zero is not allowed \n"; } void divisionbyzero::logerror() { myfile.open("error.txt",ios::app); myfile<< "Division by zero error \n"; myfile.close(); } void filenotfound::errormessage() {  cout << "File not found \n"; } void filenotfound::logerror() { myfile.open("error.txt",ios::app); myfile<< "File not found error \n"; myfile.close(); }

Step 2: Enter the following code into your favorite text editor and save it as 010-04.cpp.

#include "test.h" #include <iostream> using namespace std; // global variables divisionbyzero d; int main() {  int answer, dividend, divisor;  try {   cout << "Please enter an integer \n";   cin >> dividend;   cout << "Please enter another integer \n";   cin>> divisor;   if(divisor ==0)       throw d ;       answer = dividend/divisor;       cout << "The answer is " << answer; } catch(divisionbyzero de) {       de.errormessage();       de.logerror(); } return 0;    }

Step 3: Compile the code.

Step 4: Run the program. You should see something much like what is shown in Figure 10.5.

click to expand
Figure 10.5: Creating your own exception classes.

Hint!

You may have noticed that the functions for each of the specialized exception classes are repeated. This is a rather inelegant situation and in Chapter 11 you will see a way to correct this.

As you can see, creating such specialized exception classes allows you to handle different exceptions in a variety of ways. You simply include the header file that has the custom exception classes that you created, then you create a global instance of that class. Now you can use the methods of that class anywhere you wish. Also notice that the file object was private to both classes. This is because that object is only used inside the logerror method and there is no need for anyone to have external access to it. This gives you a great deal of flexibility in your exception handling.




C++ Programming Fundamentals
C++ Programming Fundamentals (Cyberrookies)
ISBN: 1584502371
EAN: 2147483647
Year: 2005
Pages: 197
Authors: Chuck Easttom

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net