C++ Exception Handling MCQ Quiz
Test your knowledge of C++ Exception Handling with this interactive quiz. Select the correct answer for each question and see immediate feedback.
Medium Level Questions Medium
Which of the following keywords is used to throw an exception in C++?
The throw keyword is used to raise or throw an exception in C++. The try block identifies a block of code where exceptions can occur, and catch blocks handle the exceptions.
What is the output of the following code?
try {
throw 20;
}
catch (int e) {
cout << "Exception #" << e;
}
catch (...) {
cout << "Default exception";
}
throw 20;
}
catch (int e) {
cout << "Exception #" << e;
}
catch (...) {
cout << "Default exception";
}
The code throws an integer exception with value 20, which is caught by the first catch block that handles integers. The ellipsis catch block (...) is for any exception type but is not reached in this case.
Which of the following is the base class for all standard exception classes in C++?
std::exception is the base class for all standard exceptions defined in the <exception> header. It provides a virtual member function what() that returns a descriptive string.
What happens if an exception is thrown but not caught anywhere in the program?
If an exception is not caught anywhere in the program, the C++ runtime calls the terminate() function, which by default aborts the program. This can be customized using set_terminate().
Which exception class should be used for errors that can only be detected during runtime?
std::runtime_error is the base class for exceptions that can only be detected during runtime, such as range errors or arithmetic overflow. std::logic_error is for errors preventable by proper coding.
What is the purpose of the catch-all handler catch(...)?
The catch-all handler catch(...) can catch any type of exception thrown. It's often used as a last resort to ensure no exception goes unhandled, but it should be used carefully as it doesn't provide access to the exception object.
Which of the following is NOT a standard exception class in C++?
C++ does not have a standard std::division_by_zero exception. Division by zero typically causes undefined behavior rather than throwing an exception. The other options are all standard exception classes.
What is the output of the following code?
#include <iostream>
#include <stdexcept>
using namespace std;
int main() {
try {
try {
throw runtime_error("Inner exception");
}
catch (runtime_error& e) {
cout << "Caught: " << e.what() << endl;
throw; // rethrow
}
}
catch (runtime_error& e) {
cout << "Rethrown caught: " << e.what();
}
return 0;
}
#include <stdexcept>
using namespace std;
int main() {
try {
try {
throw runtime_error("Inner exception");
}
catch (runtime_error& e) {
cout << "Caught: " << e.what() << endl;
throw; // rethrow
}
}
catch (runtime_error& e) {
cout << "Rethrown caught: " << e.what();
}
return 0;
}
Rethrown caught: Inner exception
The inner catch block catches the exception, prints "Caught: Inner exception", and then rethrows the same exception using throw; without an operand. The outer catch block then catches the rethrown exception and prints "Rethrown caught: Inner exception".
What is the purpose of the noexcept specifier in C++?
The noexcept specifier indicates that a function is not expected to throw any exceptions. If a function declared noexcept does throw an exception, std::terminate is called. This allows for optimizations and better code documentation.
Which function is called when a dynamic_cast fails when casting to a reference type?
When a dynamic_cast fails on a reference type (not pointer type), it throws a std::bad_cast exception. For pointer types, dynamic_cast returns nullptr instead of throwing an exception.
Advanced Level Questions Advanced
What is the purpose of the std::uncaught_exceptions() function introduced in C++17?
std::uncaught_exceptions() returns the number of exceptions currently being handled (i.e., for which catch blocks have been entered but not exited). This is useful for implementing destructors that behave differently depending on whether they're called during stack unwinding.
What is the main difference between exception handling in C++ and Java?
Java has checked exceptions (exceptions that must be either caught or declared in the method signature), while C++ does not have this concept. In C++, any type can be thrown as an exception, not just objects derived from std::exception.
What is the problem with throwing exceptions from destructors in C++?
If a destructor throws an exception while another exception is already active (i.e., during stack unwinding), the C++ runtime calls std::terminate(), which by default aborts the program. This is why destructors should generally not throw exceptions.
Which of the following is a valid way to create a custom exception class in C++?
All are valid approaches. The most common practice is to inherit from std::exception or one of its derived classes like std::runtime_error and override the what() method. However, any class with a suitable interface can be used as an exception.
What does the std::rethrow_exception function do?
std::rethrow_exception is used to throw an exception that was previously captured and stored in a std::exception_ptr. This is particularly useful for transferring exceptions between threads or for deferred exception handling.