Nested try-catch
Nested try-catch Blocks in C++
C++ Nested try-catch Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <stdexcept>
using namespace std;
void innerFunction(int level) {
if (level == 1) {
throw runtime_error("Error at level 1");
}
if (level == 2) {
throw invalid_argument("Error at level 2");
}
cout << "Level " << level << " processed successfully" << endl;
}
void outerFunction(int level) {
try {
cout << "Outer try: Processing level " << level << endl;
try {
cout << "Inner try: Calling innerFunction" << endl;
innerFunction(level);
} catch (const invalid_argument& e) {
cout << "Inner catch: " << e.what() << " (handled)" << endl;
// Re-throw to outer catch
throw;
}
cout << "Outer try: After inner try-catch" << endl;
} catch (const runtime_error& e) {
cout << "Outer catch: " << e.what() << " (handled)" << endl;
}
}
int main() {
cout << "=== Test 1: Error at level 1 ===" << endl;
outerFunction(1);
cout << "\n=== Test 2: Error at level 2 ===" << endl;
outerFunction(2);
cout << "\n=== Test 3: No error ===" << endl;
outerFunction(3);
return 0;
}=== Test 1: Error at level 1 === Outer try: Processing level 1 Inner try: Calling innerFunction Outer catch: Error at level 1 (handled) === Test 2: Error at level 2 === Outer try: Processing level 2 Inner try: Calling innerFunction Inner catch: Error at level 2 (handled) Outer catch: std::invalid_argument (handled) === Test 3: No error === Outer try: Processing level 3 Inner try: Calling innerFunction Level 3 processed successfully Outer try: After inner try-catch
Understanding Nested try-catch
This program teaches you how to use Nested try-catch Blocks in C++. Nested try-catch blocks allow handling exceptions at different levels of abstraction, enabling local error handling or re-throwing exceptions to outer levels.
---
1. What This Program Does
The program demonstrates nested try-catch blocks:
Nested try-catch enables hierarchical error handling.
---
2. Header Files Used
---
3. Understanding Nested try-catch
Nested Concept
:
Exception Flow
:
---
4. Inner try-catch Block
Structure
:
try {
try {
} catch (const SomeException& e) {
// Inner catch
}
} catch (const OtherException& e) {
// Outer catch
}
// Inner codeHow it works
:
---
5. Re-throwing Exceptions
Using throw
:
catch (const invalid_argument& e) {
throw; // Re-throw to outer
}
cout << "Inner catch: " << e.what() << endl;How it works
:
---
6. Exception Propagation
Flow Example
:
How it works
:
---
7. When to Use Nested try-catch
Best For
:
Example Scenarios
:
---
8. Important Considerations
Catch Order
:
Re-throwing
:
Error Context
:
---
9. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning advanced exception handling, understanding exception propagation, and preparing for hierarchical error management in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Nested try-catch
This C++ program is part of the "Exception Handling Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.