Syntax of Nested Try Blocks

The nested try/catch takes this syntax:

try
{
    // Code...... throw e2
    try
    {
        // code..... throw e1
    }
    catch (Exception e1)
    {
        // handling exception
    }
}
catch (Exception e2)
{    
    // handling exception
}

Here,

  • e1: Exception thrown in inner block.
  • e2: Exception thrown in outer block.

Nested Try Blocks in C++

In C++, a nested try block refers to the try-block nested inside another try or catch block. It is used to handle exceptions in cases where different exceptions occur in a different part of the code.

Similar Reads

Syntax of Nested Try Blocks

The nested try/catch takes this syntax:...

Example of Nested Try Blocks

C++ // C++ program to illustrate the use of nested try blocks #include 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; }...

Contact Us