CPP Exception Handling Quiz

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Thames
T
Thames
Community Contributor
Quizzes Created: 11982 | Total Attempts: 187,298
| Attempts: 12 | Questions: 19 | Updated: Aug 15, 2026
Please wait...
Question 1 / 20
🏆 Rank #-- ▾
0 %
0/100
Score 0/100

1. What is thrown when attempting to dereference a null pointer in a checked context?

Explanation

C++ does not define a standard “checked context” that turns null-pointer dereferencing into an exception. Dereferencing a null pointer has undefined behavior, so no standard exception is thrown; any diagnostic or failure depends on the implementation.

Submit
Please wait...
About This Quiz
CPP Exception Handling Quiz - Quiz

This quiz tests your understanding of exception handling in C++, a critical skill for writing robust and maintainable code. You'll explore try-catch blocks, exception types, custom exceptions, and best practices for error management. Master these concepts to build applications that gracefully handle runtime errors and unexpected conditions.

2.

What first name or nickname would you like us to use?

You may optionally provide this to label your report, leaderboard, or certificate.

2. True or False: Exceptions should be caught by const reference to avoid slicing.

Explanation

Catching an exception by `const` reference avoids copying it and prevents a derived exception object from being sliced down to its base type. The `const` qualifier also prevents accidental modification of the exception while it is being handled, while preserving access to its dynamic type and virtual behavior.

Submit

3. What happens if no catch block matches a thrown exception?

Explanation

In C++, if an exception propagates beyond the program’s active exception-handling scopes without finding a matching `catch` block, it is considered uncaught. The runtime then calls `std::terminate()`, which typically ends the program. The exception is not silently ignored, and normal execution does not continue.

Submit

4. True or False: The stack is unwound when an exception is thrown.

Explanation

When an exception is thrown, control searches for a matching handler. As it moves up the call stack, the program exits intervening functions and scopes, cleaning up their resources along the way. This process is called stack unwinding.

Submit

5. A function declared with 'throw()' specification means it____ any exceptions.

Explanation

A `throw()` specification indicates that no exceptions may escape the function. If an exception does escape, older C++ rules invoke a termination-related handler.

Submit

6. Which exception is thrown for invalid type casts using dynamic_cast?

Explanation

A failed `dynamic_cast` to a reference type throws `std::bad_cast`, indicating that the object cannot be safely converted to the requested type. When `dynamic_cast` is applied to a pointer instead, a failed cast returns `nullptr` rather than throwing an exception.

Submit

7. What is the primary purpose of the noexcept keyword in C++11 and later?

Explanation

The `noexcept` keyword indicates that a function is not expected to throw exceptions. It helps the compiler and callers reason about exception safety and can enable optimizations. If a `noexcept` function does throw, the program calls `std::terminate`.

Submit

8. True or False: An exception thrown inside a catch block cannot be caught by an outer try-catch.

Explanation

An exception thrown inside a `catch` block can be caught by an outer `try-catch` if the `catch` block is inside the outer `try` block. It cannot be caught by another `catch` clause belonging to the same `try`; that clause only handles exceptions from its associated `try` block.

Submit

9. In exception handling, the order of catch blocks matters; more____ exceptions should be caught first.

Explanation

Catch blocks are checked in order. A derived exception is more specific than its base exception, so it should be caught first; otherwise, the base-class catch may handle it before the derived-class catch can run, making the latter unreachable.

Submit

10. Which exception is thrown when dynamic memory allocation fails?

Explanation

In C++, a failed dynamic allocation using a throwing form of `new` raises `std::bad_alloc`. This exception, defined in the standard library, signals that the requested memory could not be allocated. The other listed names are not standard C++ exceptions for reporting allocation failure.

Submit

11. What keyword is used to throw an exception in C++?

Explanation

In C++, the `throw` keyword is used to signal an exception, transferring control to a matching `catch` block.

Submit

12. True or False: You can create custom exception classes by inheriting from std::exception.

Explanation

In C++, a custom exception class can inherit from `std::exception`. It can override the virtual `what()` method to provide an explanatory error message. Throwing an instance of the class allows it to be caught through a `std::exception` reference, supporting consistent handling of standard and custom exceptions.

Submit

13. A catch-all handler uses the ellipsis operator____.

Explanation

In C++, a catch-all exception handler is written as `catch(...)`. The ellipsis allows it to catch any exception type not handled by earlier, more specific `catch` clauses, so it is often used as a final fallback for cleanup or error reporting.

Submit

14. Which exception is thrown when accessing an out-of-range vector element?

Explanation

`std::vector::at()` checks whether the requested index is within the vector’s bounds and throws `std::out_of_range` if it is not. By contrast, using `operator[]` with an invalid index does not provide this exception guarantee and results in undefined behavior.

Submit

15. What does the 'what()' function do in exception handling?

Explanation

In C++, `what()` is a member function of `std::exception`. It returns a null-terminated character string describing the exception, which can be used to display or log an error message.

Submit

16. True or False: Multiple catch blocks can follow a single try block.

Explanation

A single `try` block can be followed by multiple `catch` blocks, each handling a different type of exception. When an exception occurs, the program runs the first matching `catch` block, allowing different errors to be handled appropriately.

Submit

17. In C++, a catch block can catch exceptions by____.

Explanation

A C++ exception handler can receive the thrown object either as a copy or through a reference. Catching by value creates a separate copy and may cause object slicing, while catching by reference avoids that copy and preserves the exception’s dynamic type. A const reference is commonly preferred when the handler does not need to modify the object.

Submit

18. What is the base class for all standard exceptions in C++?

Explanation

`std::exception` is the standard base class for C++ library exceptions. Many standard exception types, such as `std::runtime_error` and `std::bad_alloc`, derive from it, allowing them to be caught through a common `const std::exception&` handler.

Submit

19. Which of the following is the correct syntax for a try-catch block?

Explanation

A try-catch block uses `try` to enclose code that may cause an exception and `catch` to handle that exception. The syntax is `try { } catch { }`.

Submit
×
Saved
Thank you for your feedback!
View My Results
Cancel
  • All
    All (19)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
What is thrown when attempting to dereference a null pointer in a...
True or False: Exceptions should be caught by const reference to avoid...
What happens if no catch block matches a thrown exception?
True or False: The stack is unwound when an exception is thrown.
A function declared with 'throw()' specification means it____ any...
Which exception is thrown for invalid type casts using dynamic_cast?
What is the primary purpose of the noexcept keyword in C++11 and...
True or False: An exception thrown inside a catch block cannot be...
In exception handling, the order of catch blocks matters; more____...
Which exception is thrown when dynamic memory allocation fails?
What keyword is used to throw an exception in C++?
True or False: You can create custom exception classes by inheriting...
A catch-all handler uses the ellipsis operator____.
Which exception is thrown when accessing an out-of-range vector...
What does the 'what()' function do in exception handling?
True or False: Multiple catch blocks can follow a single try block.
In C++, a catch block can catch exceptions by____.
What is the base class for all standard exceptions in C++?
Which of the following is the correct syntax for a try-catch block?
play-Mute sad happy unanswered_answer up-hover down-hover success oval cancel Check box square blue
Alert!