try-catch Blocks


C++ offers a rather powerful error-handling tool called the try-catch block. This technique is also used in Java and VB.net™, so this is a skill you will be able to carry over to other programming languages. The try-catch block consists of two sections. The first is the code that you want to execute. This is the code that you try. If an exception is generated then the catch block catches it. Thus, the catch block is where your error handling goes. The following example illustrates this point.

Example 7.1

Step 1: Enter the following code into your favorite text editor.

#include <iostream> using namespace std;   #include <fstream> int main ()  {  try  {    char buffer[256];    ifstream myfile ("test.txt");        while (! myfile.eof() )    { myfile.getline (buffer,100); cout << buffer << endl;    }  }// end of try block  catch(…)   { cout << "There was an error !\n";   }  return 0; } 

Step 2: Compile the code.

Step 3: Run the code.

Now, if the file is found, then things will work out as they always have. But if for some reason the file is not found, then the runtime error will be caught and the user will get a simple error message. To test this you can simply change the file name to one that does not exist on your PC.

You probably also noticed that the catch block has three dots in the parentheses. This means that all exceptions should be handled by this single catch block. You will see later in this chapter that you can handle different types of exceptions differently.

Essentially, what you see in the previous example is that the code in the try block is attempted. If it executes without error, then the catch block will never be executed. If an error occurs in the try block then execution of that code stops and the catch block is executed. There should be absolutely no code whatsoever between the closing bracket of the try block and the opening bracket of the catch block.

Watchout!

If you do not use try-catch blocks, then any exception that occurs will cause your program to cease working.

It is also rather common practice for C++ programmers to set up a series of try- catch blocks, each catching a different type of error. What happens is, depending on what is wrong, your code will “throw” a different type of message, such as an int, string, and so on. You then have a series of catch blocks that catch each of these types of errors. Consider the following example.

Example 7.2

Step 1: Enter the following code into your favorite text editor.

#include <iostream> using namespace std;   int main() { int answer, divisor, dividend; try {   cout << "Please enter an integer \n";   cin >>divisor;       cout << "Please enter another integer \n";       cin >> dividend;       if(dividend ==0)      throw 0;          answer = divisor/dividend;      cout << answer;      return 0; } catch (int i) {  cout << "You cannot divide by zero"; }    }

Step 2: Compile the code.

Step 3: Run the code. Enter 0 for the second number. You should see something like what is depicted in Figure 7.1

click to expand
Figure 7.1: Multiple catch blocks.

You can see that this catch block only catches integers. If the dividend is 0, then we throw an integer. It would be a simple matter to throw a variety of exceptions using different data types for each error type.




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