Broad Network


Nesting Try-Catch Blocks

Exception Handling in C++ - Part 3

Forward: In this part of the series, we see how try-catch constructs can be nested in C++ Exceptions.

By: Chrysanthus Date Published: 24 Aug 2012

Introduction

This is part 3 of my series, Exception in C++. Try-catch constructs can be nested. In this part of the series, we see how try-catch constructs can be nested in C++ Exceptions.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Procedure
You have an outermost try-catch construct. Inside the catch block of the outermost try-catch construct you have an inner try-catch construct. Inside this inner try-catch construct you have another try-catch construct in its catch block. This chain continues. Inside each catch block, an attempt is made to solve the problem. If the problem cannot be solved, then the try-catch construct in the catch block is executed, and this may call another inner try-catch block.

Example
Let us consider the case of division again. Division by zero should not be allowed; that we know. Assume that 15 is some reserved number we should not use for the denominator. We can have a nested try-catch construct at two levels. The outer one checks the division by zero and the inner one checks the division by 15. The following code illustrates this:

#include <iostream>
using namespace std;

int main()
    {
        float numerator = 8;
        float denominator = 15;

        try
            {
                if ((denominator != 0 )&&(denominator != 15))
                    {
                        float answer = numerator/denominator;
                        cout << answer;
                    }
                else
                    {
                        if (denominator == 0 )
                            throw 0;
                        else
                                throw 15;
                    }
            }
        catch (int e)
            {
                if (e == 0)
                    cout << "Division by zero is not allowed!";

                 try
                    {
                        if (e == 15)
                            throw 15;
                    }
                catch (int eFTeen)
                    {
                        cout << "15 happens to be a reserved denominator for our division.";
                    }
            }
    
        return 0;
    }

The first two lines in the main function initialize the numerator and denominator. Then you have the nested try-catch construct. The try part of the outer try-catch construct does the division if the denominator is not zero and if it is not 15. This try part will throw zero if the denominator is 0 or it will throw 15 if the denominator is 15.

In the catch block of the inner try-catch construct an attempt is first made to solve the problem. The attempt is an if-statement, which checks if the exception (value) thrown is zero, and if it is, it reports that division by zero is not allowed. If the thrown exception is zero the inner try-catch construct will not be executed because its try block checks if the exception thrown is 15 (precisely execution will not go beyond this check of 15).

If the thrown exception is not zero it means it is 15 because the outer try block throws either zero or 15. However, in order for the inner catch block to be executed, its upper try block has to check if the exception is 15, and then throw 15.

Well, in this code, you can remove the inner try-catch construct altogether, replacing it with the else part of the if-statement in the outer catch block. My aim in this part of the series is to show you that the try-catch constructs can be nested.

Let us end here for this part of the series. We continue in the next part.

Chrys

Related Courses

C++ Course
Relational Database and Sybase
Windows User Interface
Computer Programmer – A Jack of all Trade – Poem
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message