Multiple catch blocks let you handle different kinds of errors separately. This helps your program respond correctly to each problem.
Multiple catch blocks in C++
try { // code that might throw an exception } catch (const ExceptionType1& e1) { // handle ExceptionType1 } catch (const ExceptionType2& e2) { // handle ExceptionType2 } catch (...) { // handle any other exceptions }
The try block contains code that might cause an error.
Each catch block handles a specific type of error.
try { int a = 10, b = 0; if (b == 0) throw std::runtime_error("Division by zero"); } catch (const std::exception& e) { std::cout << "Standard exception: " << e.what() << std::endl; } catch (...) { std::cout << "Unknown exception caught" << std::endl; }
try { throw 20; } catch (int e) { std::cout << "Caught an int exception: " << e << std::endl; } catch (double e) { std::cout << "Caught a double exception: " << e << std::endl; }
This program throws a runtime error and catches it with the matching catch block, printing the error message.
#include <iostream> #include <string> #include <stdexcept> int main() { try { std::string input = "error"; if (input == "error") { throw std::runtime_error("Runtime error occurred"); } else if (input == "fail") { throw std::logic_error("Logic error occurred"); } } catch (const std::runtime_error& e) { std::cout << "Caught runtime error: " << e.what() << std::endl; } catch (const std::logic_error& e) { std::cout << "Caught logic error: " << e.what() << std::endl; } catch (...) { std::cout << "Caught unknown exception" << std::endl; } return 0; }
Catch blocks are checked in order. The first matching catch block runs.
The catch(...) block catches any exception not caught by earlier blocks.
Use specific exception types first, then more general ones.
Multiple catch blocks let you handle different errors separately.
Put the try block around code that might fail.
Use catch blocks for each error type you want to handle.
Practice
What is the main purpose of using multiple catch blocks in C++?
Solution
Step 1: Understand the role of catch blocks
Catch blocks are used to handle exceptions that occur in the try block.Step 2: Recognize the benefit of multiple catch blocks
Multiple catch blocks allow handling different exception types separately, so each error can be managed properly.Final Answer:
To handle different types of exceptions separately -> Option BQuick Check:
Multiple catch blocks = handle different exceptions [OK]
- Thinking multiple catch blocks speed up code
- Confusing catch blocks with variable declarations
- Believing try blocks are not needed with multiple catch blocks
Which of the following is the correct syntax to catch any remaining exceptions in multiple catch blocks in C++?
try {
// code
}
catch (int e) {
// handle int
}
_____ {
// handle other
}Solution
Step 1: Identify the syntax for catch-all block
The catch(...) block catches any exception type not caught by previous catch blocks.Step 2: Confirm correct syntax
Only catch(...) uses ellipsis and no variable type, making it the correct syntax for a generic catch block.Final Answer:
catch (...) -> Option AQuick Check:
catch(...) = catch all exceptions [OK]
- Using catch without parentheses
- Trying to catch unknown types without ellipsis
- Misplacing catch blocks after try
What will be the output of the following C++ code?
try {
throw 10;
}
catch (double e) {
std::cout << "Double exception";
}
catch (int e) {
std::cout << "Integer exception: " << e;
}Solution
Step 1: Identify the thrown exception type
The code throws an integer exception with value 10.Step 2: Match thrown exception with catch blocks
The first catch block expects a double, so it is skipped. The second catch block matches int, so it executes and prints the message.Final Answer:
Integer exception: 10 -> Option DQuick Check:
throw int = catch int block runs [OK]
- Assuming first catch always runs
- Confusing int and double types
- Expecting no output if types differ
Find the error in this code snippet using multiple catch blocks:
try {
throw "error";
}
catch (int e) {
std::cout << "Int error";
}
catch (char e) {
std::cout << "Char error";
}Solution
Step 1: Identify the thrown exception type
The code throws a string literal, which is of type const char*.Step 2: Check catch blocks for matching types
Catch blocks handle int and char, but not const char*. So no catch block matches the thrown exception, causing an unhandled exception.Final Answer:
Missing catch block for const char* exception -> Option AQuick Check:
Thrown type must match catch parameter [OK]
- Assuming char catch handles string literals
- Thinking try block syntax is wrong
- Believing catch order matters for unrelated types
Consider this code with multiple catch blocks:
try {
throw std::runtime_error("runtime error");
}
catch (std::exception& e) {
std::cout << "Exception: " << e.what();
}
catch (std::runtime_error& e) {
std::cout << "Runtime error: " << e.what();
}What will be the output and why?
Solution
Step 1: Understand exception inheritance
std::runtime_error is derived from std::exception, so it matches both catch blocks.Step 2: Analyze catch block order
The first catch block catches std::exception by reference, so it catches the thrown runtime_error before the second catch block is reached.Step 3: Determine output
The first catch block prints "Exception: runtime error" using e.what().Final Answer:
Compilation error due to catch order -> Option CQuick Check:
Derived class catch must come before base class catch to avoid compilation error [OK]
- Expecting derived catch to run first
- Thinking catch order causes no compilation error
- Assuming no output if multiple matches
Ready to prove it?
Pass the test and this pattern moves unknown
