Multiple catch Blocks
Multiple catch Blocks for Different Exception Types in C++
C++ Multiple catch Blocks Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <stdexcept>
using namespace std;
void processNumber(int num) {
if (num < 0) {
throw invalid_argument("Number cannot be negative");
}
if (num > 100) {
throw out_of_range("Number exceeds maximum value");
}
if (num == 0) {
throw runtime_error("Number cannot be zero");
}
cout << "Processing number: " << num << endl;
}
int main() {
int numbers[] = {5, -3, 150, 0, 50};
for (int num : numbers) {
try {
processNumber(num);
} catch (const invalid_argument& e) {
cout << "Invalid argument: " << e.what() << endl;
} catch (const out_of_range& e) {
cout << "Out of range: " << e.what() << endl;
} catch (const runtime_error& e) {
cout << "Runtime error: " << e.what() << endl;
} catch (...) {
cout << "Unknown error occurred" << endl;
}
}
return 0;
}Processing number: 5 Invalid argument: Number cannot be negative Out of range: Number exceeds maximum value Runtime error: Number cannot be zero Processing number: 50
Understanding Multiple catch Blocks
This program teaches you how to use Multiple catch Blocks in C++. Multiple catch blocks allow handling different exception types with specific error handling logic. This enables precise error management based on the type of exception thrown.
---
1. What This Program Does
The program demonstrates multiple catch blocks:
Multiple catch blocks enable type-specific error handling.
---
2. Header Files Used
---
3. Understanding Multiple Catch Blocks
Multiple Catch Concept
:
Catch Order
:
---
4. Standard Exception Types
Common Types
:
How it works
:
---
5. Throwing Different Exceptions
Throwing Specific Types
:
if (num < 0) {
throw invalid_argument("Number cannot be negative");
}
if (num > 100) {
throw out_of_range("Number exceeds maximum value");
}
How it works
:
---
6. Catching Specific Exceptions
Multiple Catch Blocks
:
catch (const invalid_argument& e) {
} catch (const out_of_range& e) {
} catch (const runtime_error& e) {
}
cout << "Invalid argument: " << e.what() << endl;How it works
:
---
7. Catch-All Block
Using catch(...)
:
catch (...) {
}
cout << "Unknown error occurred" << endl;How it works
:
---
8. When to Use Multiple Catch Blocks
Best For
:
Example Scenarios
:
---
9. Important Considerations
Catch Order
:
Exception Hierarchy
:
Error Messages
:
---
10. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning advanced exception handling, understanding exception types, and preparing for robust 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 Multiple catch Blocks
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.