Chapter 12 - Exception Handling in C++

An exception is a situation in which a program has an unexpected circumstance that the section of code containing the problem is not explicitly designed to handle. In C++, exception handling is useful because it makes it easy to separate the error handling code from the code written to handle the chores of the program. This chapter presents the fundamentals of dealing with problems that arises in C++ programs.

 

What is exceptions?

 

Exceptions provide a way to react to exceptional circumstances (like runtime errors) in programs by transferring control to special functions called handlers.

To catch exceptions, a portion of code is placed under exception inspection. This is done by enclosing that portion of code in a try-block. When an exceptional circumstance arises within that block, an exception is thrown that transfers the control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored. C++ exception handling is built upon three keywords: try, catch, and throw.

 

try: A try block identifies a block of code for which particular exceptions will be activated. It's followed by one or more catch blocks called exception handler.

catch: A program catches an exception with an exception handler at the place in a program where the programmer want to handle the problem. The catch keyword indicates the catching of an exception.

throw: A program throws an exception when a problem shows up. This is done using a throw keyword.

 

How exceptions are used

 

try blocks are created to surround areas of code that may have a problem. For example:

try

{

<SomeDangerousCodes>

}

 

catch blocks handle the exceptions thrown in the try block. For example:

try

{

<SomeDangerousCodes>

}

catch(OutOfMemory)

{

// take some actions

}

catch(FileNotFound)

{

// take other action

}

 

The basic steps in using exceptions are:

1.Identify those areas of the program in which an operation that might raise an exception, and put them in try blocks.

2.Create catch blocks to catch the exceptions if they are thrown, to clean up allocated memory, and to inform the user as appropriate.

 

Figure 12.1 presents a sample program that uses exception handling.

 

Figure 12.1 (source file name: ch12sample1.cpp)

 

Lines 8 to 21 presents the try catch block exception handling. In lines 11 to 14, it presents how the throw keyword is use. In this example, the exception is a user-defined exception, determining if the entered number from the cin is less than 0 or greater than 10. Lines 12 and 14 throws the error message. This error message is then pass to the catch block in line 18. The const char* parameter in catch keyword in line 18 is a standard try catch error message parameter, which its value is based from what is thrown in the throw keyword. The errMsg is then printed if an error occurs.

Figures 12.2 to 12.4 presents the possible outputs of the program.

 

Figure 12.2

 

Figure 12.3

 

Figure 12.4

 

Code listings of figure 12.1

#include<iostream>

using namespace std;

 

int main()

{

int iVal;

cout<<"Enter a number:";

try

{

cin>>iVal;

if(iVal<0)

throw "Number cannot be Negative";

if(iVal>10)

throw "Number cannot be Greater than 10";

cout<<iVal<<" Multiplied by 3 is "<<iVal*3;

}

catch (const char* errMsg)

{

cout<<errMsg;

}

fflush(stdin);

getchar();

return 0;

}

 

Self-assessment questions

 

Answer the following

1. Why bother with raising exceptions? Why not handle the error right where it happens?

 

2. Does an exception have to be caught in the same place where the try block created the exception?