Exception-Handling Overview in C++

An exception is thrown using a throw statement and caught in a try-catch block.

To demonstrate exception handling including how an exception is created and thrown, let us begin with an example that reads in two integers and displays their quotient, as shown in Listing 16.1.

Listing 16.1 Quotient.cpp

1 #include <iostream>
2
using namespace std;
3
4
int main()
5 {
6   
// Read two integers
7    cout << “Enter two integers: “;
8   
int number1, number2;
9    cin >> number1 >> number2;
10
11    cout << number1 <<
” / ” << number2 << ” is ”
12    << (number1 / number2) << endl;
13
14   
return 0;
15 }

If you enter 0 for the second number, a runtime error occurs, because you cannot divide an integer by 0. (Recall that a floating-point number divided by 0 does not raise an exception.) A simple way to fix the error is to add an if statement to test the second number, as shown in Listing 16.2.

Listing 16.2 QuotientWithIf.cpp

1 #include <iostream>
2
using namespace std;
3
4
int main()
5 {
6   
// Read two integers
7     cout << “Enter two integers: “;
8     
int number1, number2;

9     cin >> number1 >> number2;
10
11   
if (number2 != 0)
12    {
13       cout << number1 <<
” / ” << number2 << ” is ”
14       << (number1 / number2) << endl;
15    }
16   
else
17    {
18        cout <<
“Divisor cannot be zero” << endl;
19    }
20
21   
return 0;
22 }

To demonstrate the concept of exception handling including how to create, throw, catch, and handle an exception, we rewrite Listing 16.2 as shown in Listing 16.3

Listing 16.3 QuotientWithException.cpp

1 #include <iostream>
2
using namespace std;
3
4
int main()
5 {
6     
// Read two integers
7     cout << “Enter two integers: “;
8     
int number1, number2;
9     cin >> number1 >> number2;
10
11   
try
12    {
13       
if (number2 == 0)
14       
throw number1;
15
16        cout << number1 <<
” / ” << number2 << ” is ”
17        << (number1 / number2) << endl;
18     }
19     
catch (int ex)
20     {
21        cout <<
“Exception: an integer ” << ex <<
22       
” cannot be divided by zero” << endl;
23     }
24
25    cout <<
“Execution continues …” << endl;
26
27   
return 0;
28 }

The program contains a try block and a catch block. The try block (lines 11-18) contains the code that is executed in normal circumstances. The catch block contains the code that is executed when number2 is zero. When number2 is zero, the program throws an exception by executing

 throw number1;

The value thrown, in this case numberl, is called an exception. The execution of a throw statement is called throwing an exception. You can throw a value of any type. In this case, the value is of the int type.

When an exception is thrown, the normal execution flow is interrupted. As the name sug­gests, to “throw an exception” is to pass the exception from one place to another. The excep­tion is caught by the catch block. The code in the catch block is executed to handle the exception. Afterward, the statement (line 25) after the catch block is executed.

The throw statement is analogous to a function call, but instead of calling a function, it calls a catch block. In this sense, a catch block is like a function definition with a parameter that matches the type of the value being thrown. However, after the catch block has been executed, the program control does not return to the throw statement; instead, it executes the next statement after the catch block.

The identifier ex in the catch block header

catch (int ex)

catch block parameter acts very much like a parameter in a function. So, it is referred to as a catch block parameter.

The type (e.g., int) preceding ex specifies the kind of exception the catch block can catch. Once the exception is caught, you can access the thrown value from this parameter in the body of a catch block.

In summary, a template for a try-throw-catch block may look like this:

try

{

   Code to try;

   Throw an exception with a throw statement or

     from function if necessary;

   More code to try;

}

catch (type ex)

{

   Code to process the exception;

}

An exception may be thrown directly using a throw statement in a try block, or a function may be invoked that throws an exception.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *