Nested Try Blocks in C++

Last Updated : 3 Jul, 2026

A nested try block in C++ is a try block placed inside another try block or inside a catch block. Nested try blocks are useful when different parts of a program can throw different types of exceptions and require separate handling mechanisms.

  • Allows exceptions to be handled at multiple levels.
  • Enables inner exceptions to be handled locally while propagating others to outer handlers.

Example: Exception Handled by the Inner Catch Block

C++
#include <iostream>
using namespace std;

// function throwing exceptions
void func(int n)
{
    if (n < 10) {
        throw 22;
    }
    else {
        throw 'c';
    }
}

// driver code
int main()
{
    try {
        try {
            cout << "Throwing exception from inner try "
                    "block\n";
            func(2);
        }
        catch (int n) {
            cout << "Inner Catch Block caught the exception"
                 << endl;
        }
    }
    catch (char c) {
        cout << "Outer catch block caught the exception"
             << endl;
    }

    cout << "Out of the block";

    return 0;
}

Output
Throwing exception from inner try block
Inner Catch Block caught the exception
Out of the block

Explanation: In this example, func(2) throws an integer exception. Since the inner catch block is designed to handle int exceptions, the exception is caught immediately, and control does not propagate to the outer catch block.

Syntax

try {
// Outer try block

try {
// Inner try block
throw e1;
}

catch (ExceptionType1 e1) {
// Handle inner exception
}

throw e2;
}
catch (ExceptionType2 e2) {
// Handle outer exception}

Here:

  • e1 represents the exception thrown inside the inner try block.
  • e2 represents the exception thrown inside the outer try block.

Example: Exception Propagated to the Outer Catch Block

If the inner catch block cannot handle the exception, the exception propagates outward until a matching handler is found.

C++
#include <iostream>
using namespace std;

// function throwing exceptions
void func(int n)
{
    if (n < 10) {
        throw 22;
    }
    else {
        throw 'c';
    }
}

// driver code
int main()
{
    try {
        try {
            cout << "Throwing exception from inner try "
                    "block\n";
            func(12);
        }
        catch (int n) {
            cout << "Inner Catch Block caught the exception"
                 << endl;
        }
    }
    catch (char c) {
        cout << "Outer catch block caught the exception"
             << endl;
    }

    cout << "Out of the block";

    return 0;
}

Output
Throwing exception from inner try block
Outer catch block caught the exception
Out of the block

Explanation: Here, func(12) throws a character exception. Since the inner catch block handles only integer exceptions, the exception propagates to the outer catch block, which successfully handles it.

How Nested Try Blocks Work

The exception handling mechanism in nested try blocks works as follows:

  • The exception is first searched within the current try block.
  • If no matching catch block is found, the exception propagates to the enclosing try block.
  • This process continues until a matching handler is found.
  • If no handler exists, the program terminates by calling std::terminate().

Nested Try Blocks Inside Catch Blocks

A try block can also be placed inside a catch block. This is useful when exception handling code itself may throw additional exceptions.

C++
try { 
    throw 10; 
    
} 
catch (int x) {
    try { 
        throw "Another exception"; 
        
    } 
    catch (const char* msg) {
        cout << msg; 
        
    } 
    
}

Applications of Nested Try Blocks

Nested try blocks are commonly used in the following situations:

  • Handling different exception types at different levels.
  • Separating local and global exception handling logic.
  • Performing additional error handling inside catch blocks.
  • Implementing layered exception propagation in large applications.
Comment