Topic: Programming in C++
What is the output of the following C++ program?
#include <iostream>
int main() {
try {
throw 20;
} catch (double e) {
std::cout << "double";
} catch (...) {
std::cout << "ellipsis";
}
return 0;
}
- A. Program terminates abruptly
- B. Compilation error
- C. double
- D. ellipsis
Answer: D. ellipsis
Explanation: The statement `throw 20;` throws an exception of type `int`. The first catch block `catch (double e)` specifically catches exceptions of type `double` and ignores `int` exceptions (C++ does not perform implicit type casting during exception matching). The `catch (...)` block is a universal catch-all handler that catches any thrown exception not caught previously, so it executes and prints "ellipsis".