C++ Exception Handling Fundamentals Quiz Quiz

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.

  1. Identifying the Purpose of try Blocks

    What is the main purpose of a try block in C++ exception handling?

    1. To terminate the program safely
    2. To catch and handle exceptions thrown by other code
    3. To define custom exception classes
    4. To enclose code that might throw an exception

    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.

  2. Syntax of Throwing Exceptions

    Which statement correctly throws an integer exception in C++?

    1. except 404;
    2. throw 404;
    3. raise 404;
    4. emit 404;

    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.

  3. Catching Exceptions by Reference

    Why is it recommended to catch exceptions by reference in C++, such as 'catch (const std::exceptionu0026 e)'?

    1. It always terminates the program
    2. It is required for all primitive type exceptions
    3. It avoids object slicing and unnecessary copying
    4. It makes exceptions uncatchable

    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.

  4. Standard Exception Hierarchy

    Which standard C++ class serves as the base for all standard exception classes?

    1. std::error
    2. std::exception
    3. std::throwable
    4. std::catcher

    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.

  5. Multiple Catch Blocks

    What happens if multiple catch blocks can handle a thrown exception in C++?

    1. Only the first matching catch block is executed
    2. All catch blocks are skipped
    3. Catch blocks are chosen randomly
    4. Every matching catch block is executed sequentially

    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.

  6. Uncaught Exceptions

    What is the default behavior if a C++ exception is thrown and not caught by any catch block?

    1. The program calls std::terminate and ends abruptly
    2. The operating system automatically handles the exception
    3. The exception is ignored and the program continues
    4. The exception is logged but execution resumes

    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.

  7. Handling All Exceptions

    Which catch block pattern catches any exception, regardless of its type, in C++?

    1. catch (all)
    2. catch (...)
    3. catch (std::any)
    4. catch (exception)

    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.

  8. Custom Exception Classes

    When defining a custom exception class in C++, which of the following is considered good practice?

    1. Inherit from std::string
    2. Do not inherit from any class
    3. Inherit from std::error_code
    4. Inherit from std::exception

    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.

  9. Resuming Execution After Exception

    After a C++ exception is caught and handled by a catch block, what happens to the program flow?

    1. Execution resumes after the entire try-catch block
    2. All remaining catch blocks execute
    3. The program restarts from the main function
    4. Execution continues from right before the exception was thrown

    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.

  10. Exception-Safe Resource Management

    What C++ feature helps manage resources safely when exceptions occur, ensuring objects are cleaned up automatically?

    1. Automatic Pointer Deletion (APD)
    2. Resource Acquisition Is Initialization (RAII)
    3. Garbage Collector
    4. try-finally Statement

    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.