Home C++ Introduction Decisions Loops Input/Output Functions Stack and Heap References Arrays Searching and Sorting Recursion Pointers Character and Strings Structures Classes Inheritance Exceptions Templatess STL Modern C++ Misc Books ----


Exceptions


Contents

Nested Catch

We can have nested try catch blocks also.
File: except4.cpp
#include <iostream>
#include <fstream>

using namespace std;

void function2()
{
    throw 10 ;
}

void function1()
{
        function2() ;
}

int main()
{
            try
            {
                try
                {
                    function1() ;
                }
                catch ( int& e1 )
                {
                        cout << "Integer Exception"   << endl ;
                        throw e1 ;
                }
            }
            catch( int& e2 )
            {
                cout << "Outer Integer Exception"   << endl ;
            }
    return 0;
}
Output:

$ g++ except4.cpp ; ./a.exe

Integer Exception
Outer Integer Exception
Exercises
1) What does the below program print ?
File: ex1.cpp

#include <iostream>
#include <fstream>

using namespace std;

void function2()
{
    try
     {
         cout << "Inside function2 1." << endl ;
         throw 10 ;
         cout << "Inside function2 2." << endl ;
     }
     catch( int x1 )
     {
         cout << "Inside function2 catch." << endl ;
         throw x1 ;
     }
}

void function1()
{
    try
    {
        cout << "Inside function1 1." << endl ;
        function2() ;
    }
    catch( const char* str1 )
    {
        cout << "Inside function1 catch." << endl ;
    }
}

int main()
{
    try
    {
        try
        {
            function1() ;
        }
        catch ( int& e1 )
        {
            cout << "Integer Exception:"   << e1 << endl ;
            throw e1 ;
        }
    }
    catch( int& e2 )
    {
        cout << "Outer Integer Exception"   << endl ;
    }
    return 0;
}
2) Complete the TODO sections in the following exercise
File: ex2.cpp

#include <iostream>
#include <fstream>

using namespace std;
/* This functions takes 2 values from the user and if any value
is out of range throws an exception.*/

void function1(  )
{
    int low ;
    int high ;
    cout << "Enter low value:" << endl ;
    cin >> low ;
    cout << "Enter high value:" << endl ;
    cin >> high ;
    if ( low < 0 || high > 100 )
     throw "Values are out of range." ;
}

int main()
{
        //TO DO
        //Put a try catch around fucntion1 to catch the error
        function1() ;
        return 0;
}
3) What does the below program print ?
File: nested1.cpp

#include <iostream>
#include <fstream>

using namespace std;


void function1()
{
    try
    {
        cout << "Inside function1 1." << endl ;
        throw 2 ;
    }
    catch( const char* str1 )
    {
        cout << "Inside function1 catch." << endl ;
    }
}

int main()
{
    try
    {
        try
        {
            function1() ;
        }
        catch ( int& e1 )
        {
            try
             {
               cout << "Integer Exception:"   << e1 << endl ;
               throw e1 ;
             }
           catch( int& f1 )
             {
                cout << "Integer Exception:"   << e1 << endl ;
                throw f1 ;
             }
        } //catch
    }
    catch( int& e2 )
    {
        cout << "Outer Integer Exception"   << endl ;
    }
    return 0;
}