Multiple catch Blocks in C++
Multiple catch Blocks for Different Exception Types in C++.
IntermediateException Handling ProgramsExample 2 of 5
multiple-catch-blocks.cpp
1#include <iostream>2#include <stdexcept>3using namespace std;45void processNumber(int num) {6 if (num < 0) {7 throw invalid_argument("Number cannot be negative");8 }9 if (num > 100) {10 throw out_of_range("Number exceeds maximum value");11 }12 if (num == 0) {13 throw runtime_error("Number cannot be zero");14 }1516 cout << "Processing number: " << num << endl;17}1819int main() {20 int numbers[] = {5, -3, 150, 0, 50};2122 for (int num : numbers) {23 try {24 processNumber(num);25 } catch (const invalid_argument& e) {26 cout << "Invalid argument: " << e.what() << endl;27 } catch (const out_of_range& e) {28 cout << "Out of range: " << e.what() << endl;29 } catch (const runtime_error& e) {30 cout << "Runtime error: " << e.what() << endl;31 } catch (...) {32 cout << "Unknown error occurred" << endl;33 }34 }3536 return 0;37}
Output
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
What's going on
The program demonstrates multiple catch blocks:
Handling different exception types
Specific catch blocks for each type
Catch-all block for unknown exceptions
Processing multiple values with error handling
Multiple catch blocks enable type-specific error handling.
Header Files Used
1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <stdexcept>
Provides standard exception classes.
Understanding Multiple Catch Blocks
Multiple Catch Concept
:
Different catch blocks for different types
Checked in order (top to bottom)
First matching catch executes
More specific before general
Catch Order
:
Specific exceptions first
General exceptions last
catch(...) as catch-all
Order matters!
Standard Exception Types
Common Types
:
invalid_argument: invalid parameter
out_of_range: value out of range
runtime_error: runtime error
logic_error: logic error
How it works
:
Each type for specific errors
Provides what() method
Inherits from exception
Standard error handling
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
:
Throw specific exception types
Each type for specific error
Provides error message
Caught by matching catch
Catching Specific Exceptions
Multiple Catch Blocks
:
catch (const invalid_argument& e) {
} catch (const out_of_range& e) {
cout << "Out of range: " << e.what() << endl;
} catch (const runtime_error& e) {
cout << "Runtime error: " << e.what() << endl;
}
1cout << "Invalid argument: " << e.what() << endl;
How it works
:
Each catch handles specific type
Checked in order
First match executes
Others skipped
Catch-All Block
Using catch(...)
:
catch (...) {
}
1cout << "Unknown error occurred" << endl;
How it works
:
Catches any exception type
Must be last catch block
No exception value access
Safety net for unexpected errors
When to Use Multiple Catch Blocks
Best For
:
Different error types
Type-specific handling
Precise error messages
Different recovery strategies
Comprehensive error handling
Example Scenarios
:
Input validation (multiple checks)
File operations (different errors)
Network operations (various failures)
Database operations (different errors)
Important Considerations
Catch Order
:
More specific first
General last
catch(...) always last
Wrong order = wrong catch
Exception Hierarchy
:
Standard exceptions inherit from exception
Can catch base class
More general catch catches derived
Understand inheritance
Error Messages
:
Use what() method
Provides error description
Help with debugging
Inform users
return 0;
This ends the program successfully.