Recall & Review
beginner
What does it mean to throw an exception in C++?
Throwing an exception means signaling that an error or unexpected event has occurred, by using the
throw keyword followed by an object representing the error.Click to reveal answer
beginner
How do you throw an exception with a message in C++?
You can throw an exception by writing <code>throw std::runtime_error("Error message");</code> where <code>std::runtime_error</code> is a standard exception class that takes a message.Click to reveal answer
beginner
What happens after an exception is thrown?
The program stops normal execution and looks for a matching
catch block to handle the exception. If none is found, the program usually terminates.Click to reveal answer
intermediate
Can you throw any type of object as an exception in C++?
Yes, C++ allows throwing any type of object, but it is recommended to throw objects derived from
std::exception for consistency and better error information.Click to reveal answer
beginner
Write a simple C++ code snippet that throws and catches an exception.
<pre>#include <iostream>
#include <stdexcept>
try {
throw std::runtime_error("Something went wrong");
} catch (const std::exception &e) {
std::cout << "Caught exception: " << e.what() << std::endl;
}</pre>Click to reveal answer
Which keyword is used to throw an exception in C++?
✗ Incorrect
The
throw keyword is used to signal an exception in C++.What happens if an exception is thrown but not caught?
✗ Incorrect
If no matching
catch block is found, the program usually terminates.Which of these is a recommended base class for exceptions in C++?
✗ Incorrect
std::exception is the standard base class for exceptions.How do you catch an exception of type
std::runtime_error?✗ Incorrect
You catch exceptions by specifying the type in the
catch block.Which block is used to write code that might throw an exception?
✗ Incorrect
The
try block contains code that may throw exceptions.Explain how throwing and catching exceptions works in C++.
Think about how the program stops normal work and looks for a handler.
You got /5 concepts.
Write a simple example of throwing an exception with a message and catching it.
Use std::runtime_error and std::exception for your example.
You got /4 concepts.