Complete the code to throw an exception with the message "Error occurred".
throw [1];The throw keyword is followed by the exception object or value to throw. Here, a string literal "Error occurred" is thrown as an exception.
Complete the code to throw a standard exception object.
throw [1]();std:: namespace prefix.The standard exception class in C++ is std::exception. Throwing std::exception() creates and throws a default exception object.
Fix the error in the code to correctly throw an integer exception.
throw [1];To throw an integer exception, you throw the integer value directly without quotes. Quotes would make it a string literal, which is incorrect here.
Fill both blanks to throw a logic_error exception with a message.
throw [1]("[2] error");
std:: prefix.The std::logic_error class is used to throw logic errors. The constructor takes a string message describing the error.
Fill all three blanks to throw a custom exception with a message.
class MyException : public std::exception { public: const char* what() const noexcept override { return "[1]"; } }; throw [2](); // Throw the custom exception // Catch block example try { // code that throws } catch (const [3]& e) { std::cout << e.what(); }
what() method correctly.what().The custom exception class MyException overrides the what() method to return a custom message. We throw an object of MyException and catch it by reference.