#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;
}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
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:
- Inner and outer try-catch blocks
- Handling exceptions at different levels
- Re-throwing exceptions
- Exception propagation
Nested try-catch enables hierarchical error handling.
2. Header Files Used
-
#include <iostream>
- Provides cout and cin for input/output operations.
-
#include <stdexcept>
- Provides standard exception classes.
3. 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
4. Inner try-catch Block
Structure:
try { try { // Inner code } catch (const SomeException& e) { // Inner catch } } catch (const OtherException& e) { // Outer catch }
How it works:
- Inner try-catch handles first
- Can catch specific types
- Can re-throw if needed
- Outer handles if not caught
5. Re-throwing Exceptions
Using throw:
catch (const invalid_argument& e) { cout << "Inner catch: " << e.what() << endl; throw; // Re-throw to outer }
How it works:
- throw; re-throws same exception
- Propagates to outer catch
- Preserves exception type
- Enables multi-level handling
6. Exception Propagation
Flow Example:
- Exception thrown in inner try
- Inner catch handles or re-throws
- If re-thrown, outer catch handles
- Program continues after outer catch
How it works:
- Exceptions propagate upward
- Each level can handle
- Or pass to next level
- Hierarchical error handling
7. 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
8. 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
9. return 0;
This ends the program successfully.
Summary
- Nested try-catch: handle exceptions at different levels, inner and outer blocks.
- Inner catch can handle exception or re-throw using throw; to outer catch.
- Re-thrown exceptions propagate to outer catch blocks.
- Useful for handling errors at appropriate abstraction levels.
- Understanding nested try-catch enables hierarchical error handling.
- Essential for multi-level error management and exception transformation.
This program is fundamental for learning advanced exception handling, understanding exception propagation, and preparing for hierarchical error management in C++ programs.