Explore the essentials of exception handling in C++ with this quiz, designed to reinforce knowledge of try-catch blocks, throw statements, exception classes, and safe program termination. Assess your understanding of how C++ handles runtime errors, promotes robust code, and ensures graceful error management.
What is the main purpose of a try block in C++ exception handling?
Explanation: A try block is used to enclose code that might generate an exception during execution. The catch block follows the try block and handles the exception if one is thrown. Defining custom exception classes is unrelated to the try block itself. While exceptions help programs terminate gracefully if needed, the primary purpose of a try block is not to terminate programs.
Which statement correctly throws an integer exception in C++?
Explanation: The 'throw' keyword is used in C++ to signal that an exception has occurred, followed by the value or object to be thrown. 'raise', 'except', and 'emit' are not valid C++ keywords for exception handling; they either come from other programming languages or are simply incorrect.
Why is it recommended to catch exceptions by reference in C++, such as 'catch (const std::exceptionu0026 e)'?
Explanation: Catching exceptions by reference prevents object slicing and reduces the overhead of copying potentially large exception objects. The other options are incorrect: catching by reference does not automatically terminate the program or make exceptions uncatchable, and primitive types do not specifically require this approach.
Which standard C++ class serves as the base for all standard exception classes?
Explanation: std::exception is the root class of the standard exception hierarchy in C++. The classes std::error, std::catcher, and std::throwable do not exist in the C++ standard library, though they sound plausible but are incorrect.
What happens if multiple catch blocks can handle a thrown exception in C++?
Explanation: C++ executes only the first catch block that matches the thrown exception's type. The remaining catch blocks are skipped, regardless of whether they could also handle the exception. Executing every matching catch block, skipping all catch blocks, or random selection are not consistent with the language's design.
What is the default behavior if a C++ exception is thrown and not caught by any catch block?
Explanation: If no matching catch block exists for a thrown exception, the program calls std::terminate, ending execution. Ignoring the exception or having the OS handle it is incorrect in standard C++. Logging the exception without termination is not the default behavior.
Which catch block pattern catches any exception, regardless of its type, in C++?
Explanation: The catch (...) syntax in C++ can catch any type of exception, regardless of its type. catch (std::any) and catch (all) are not valid catch patterns. catch (exception) refers only to a specific type and not to all exceptions.
When defining a custom exception class in C++, which of the following is considered good practice?
Explanation: Inheriting from std::exception allows custom exceptions to integrate with the standard exception handling mechanism, enabling compatibility with catch blocks for standard exceptions. Inheriting from std::string or std::error_code is not appropriate for exception classes, and not inheriting from any class loses the benefits of polymorphic exception handling.
After a C++ exception is caught and handled by a catch block, what happens to the program flow?
Explanation: When an exception is handled by a catch block, execution resumes at the point following the try-catch construct. It does not jump back to the statement that caused the exception, nor does it restart the main function. Only the first matching catch block is executed, not all remaining ones.
What C++ feature helps manage resources safely when exceptions occur, ensuring objects are cleaned up automatically?
Explanation: RAII leverages object lifetime to manage resources, ensuring they are properly released when exceptions occur and objects go out of scope. APD is not a recognized feature in C++. C++ does not use a garbage collector by default, and it does not have a native try-finally statement like some other languages.