Nested Try Blocks In C++ - C++ Programming Tutorial
C++ Course / Exception Handling / Nested Try Blocks In C++

Nested Try Blocks In C++

BLUF: Mastering Nested Try Blocks In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Nested Try Blocks In C++

C++ is renowned for its efficiency. Learn how Nested Try Blocks In C++ enables low-level control and high-performance computing in the tutorial below.

In this article, we will discuss the nested try blocks in C++ with its syntax and examples.

What is Nested Try Blocks?

A "nested try block" in C++ refers to a try-block that is enclosed within another try or catch block. It is employed to manage different exceptions that may occur at different points in the code.

Structure of Nesting Try Blocks:

Here is the syntax for the nested try/catch:

Example

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

In this case,

e1: The inner block threw an exception.

e2: The outer block has an exception thrown.

Example of Nested Try Blocks:

Let's consider a scenario to demonstrate the nested try blocks in C++.

Example

#include <iostream>
using namespace std;

// function throwing exceptions
void divide(int a, int b) {
 if (b == 0) {
 throw "Division by zero is not allowed";
 } else {
 throw 42;
 }
}

// driver code
int main() {
 try {
 try {
 cout << "Throwing exception from inner try block\n";
 divide(10, 0);
 } catch (const char* error) {
 cout << "Inner Catch Block caught the exception: " << error << endl;
 }
 } catch (int result) {
 cout << "Outer catch block caught the exception: " << result << endl;
 }

 cout << "Out of the block";

 return 0;
}

Output:

Output

Throwing exception from inner try block
Inner Catch Block caught the exception: Division by zero is not allowed
Out of the block

Explanation:

Here, a pair of exceptions for the int and char data types were raised through the func method. We introduced an internal try-catch block to manage the integer exceptions. Presently, the program exits the inner block and extends outward through the layers until it locates the appropriate catch block whenever an exception is triggered within any of the try blocks. In this particular scenario, the exception was caught by the internal catch block.

In the event that the outer catch block is structured to manage errors, let's observe the outcome when a character exception is thrown.

Example:

Example

#include <iostream>
using namespace std;

// function throwing exceptions
void divide(int a, int b) {
 if (b == 0) {
 throw "Division by zero is not allowed";
 } else {
 throw 'X';
 }
}

// driver code
int main() {
 try {
 try {
 cout << "Throwing exception from inner try block\n";
 divide(8, 0);
 } catch (const char* error) {
 cout << "Inner Catch Block caught the exception: " << error << endl;
 }
 } catch (char symbol) {
 cout << "Outer catch block caught the exception: " << symbol << endl;
 }

 cout << "Out of the block";

 return 0;
}

Output:

Output

Throwing exception from inner try block
Inner Catch Block caught the exception: Division by zero is not allowed
Out of the block

Explanation:

In this scenario, the exception was accurately caught by the external catch block.

Similarly, it is possible to nest try blocks within the catch block.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience