Complete the code to catch an exception thrown inside the try block.
try { throw std::runtime_error("Error occurred"); } catch ([1]) { std::cout << "Exception caught" << std::endl; }
The catch block should catch exceptions by reference to std::exception or its derived classes to handle standard exceptions properly.
Complete the code to throw a standard exception with a message.
void test() {
[1] std::runtime_error("Something went wrong");
}try or catch instead of throw.The throw keyword is used to raise an exception in C++.
Fix the error in the catch block to properly catch all exceptions.
try { // some code } catch ([1]) { std::cout << "Caught an exception" << std::endl; }
Using ... in catch block catches all exceptions regardless of type.
Fill both blanks to catch a standard exception and print its message.
try { throw std::runtime_error("Error"); } catch ([1]) { std::cout << e.[2]() << std::endl; }
message().Catch the exception by reference and use the what() method to get the error message.
Fill all three blanks to throw and catch a custom exception with a message.
class MyException : public std::exception { public: const char* [1]() const noexcept override { return "Custom error"; } }; int main() { try { [2] MyException(); } catch ([3]) { std::cout << e.what() << std::endl; } return 0; }
what() correctly.catch keyword incorrectly.The method what() returns the error message. Use throw to raise the exception and catch it by reference.