Nested try-catch in C++
Nested try-catch Blocks in C++.
IntermediateException Handling ProgramsExample 5 of 5
nested-try-catch.cpp
1#include <iostream>2#include <stdexcept>3using namespace std;45void innerFunction(int level) {6 if (level == 1) {7 throw runtime_error("Error at level 1");8 }9 if (level == 2) {10 throw invalid_argument("Error at level 2");11 }12 cout << "Level " << level << " processed successfully" << endl;13}1415void outerFunction(int level) {16 try {17 cout << "Outer try: Processing level " << level << endl;1819 try {20 cout << "Inner try: Calling innerFunction" << endl;21 innerFunction(level);22 } catch (const invalid_argument& e) {23 cout << "Inner catch: " << e.what() << " (handled)" << endl;24 // Re-throw to outer catch25 throw;26 }2728 cout << "Outer try: After inner try-catch" << endl;29 } catch (const runtime_error& e) {30 cout << "Outer catch: " << e.what() << " (handled)" << endl;31 }32}3334int main() {35 cout << "=== Test 1: Error at level 1 ===" << endl;36 outerFunction(1);3738 cout << "\n=== Test 2: Error at level 2 ===" << endl;39 outerFunction(2);4041 cout << "\n=== Test 3: No error ===" << endl;42 outerFunction(3);4344 return 0;45}
Output
=== 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
What's going on
The program demonstrates nested try-catch blocks:
Inner and outer try-catch blocks
Handling exceptions at different levels
Re-throwing exceptions
Exception propagation
Nested try-catch enables hierarchical error handling.
Header Files Used
1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <stdexcept>
Provides standard exception classes.
Understanding Nested try-catch
Nested Concept
:
try-catch inside another try-catch
Multiple levels of handling
Inner can handle or re-throw
Outer handles if inner doesn't
Exception Flow
:
Exception thrown in inner try
Inner catch can handle
Or re-throw to outer
Outer catch handles if re-thrown
Inner try-catch Block
Structure
:
try {
try {
} catch (const SomeException& e) {
// Inner catch
}
} catch (const OtherException& e) {
// Outer catch
}
1// Inner code
How it works
:
Inner try-catch handles first
Can catch specific types
Can re-throw if needed
Outer handles if not caught
Re-throwing Exceptions
Using throw
:
catch (const invalid_argument& e) {
throw; // Re-throw to outer
}
1cout << "Inner catch: " << e.what() << endl;
How it works
:
throw; re-throws same exception
Propagates to outer catch
Preserves exception type
Enables multi-level handling
Exception Propagation
Flow Example
:
1.Exception thrown in inner try
2.Inner catch handles or re-throws
3.If re-thrown, outer catch handles
4.Program continues after outer catch
How it works
:
Exceptions propagate upward
Each level can handle
Or pass to next level
Hierarchical error handling
When to Use Nested try-catch
Best For
:
Multi-level error handling
Different abstraction levels
Local error recovery
Hierarchical error management
Transforming exceptions
Example Scenarios
:
Function call chains
Library and application levels
Different error contexts
Error transformation
Multi-level recovery
Important Considerations
Catch Order
:
Inner catch handles first
Outer catch handles if re-thrown
Specific before general
Order matters
Re-throwing
:
Use throw; to re-throw
Preserves exception type
Can transform before re-throw
Enables multi-level handling
Error Context
:
Each level adds context
Better error messages
Appropriate handling level
Clear error semantics
return 0;
This ends the program successfully.