C++ | Exception Handling | Question 12
What happens when a function throws an error but doesn’t specify it in the list of exceptions it can throw.
For example, what is the output of following program?
#include <iostream> using namespace std; // Ideally it should have been "int fun() (int)" int fun() { throw 10; } int main() { try { fun(); } catch (int ) { cout << "Caught"; } return 0; } |
chevron_right
filter_none
(A) Compiler Error
(B) No compiler Error. Output is “Caught”
Answer: (B)
Explanation: C++ compiler doesn’t check enforce a function to list the exceptions that it can throw. In Java, it is enforced.
It is up to the programmer to specify. Being a civilized programmer, a programmer should specify the list.
Quiz of this Question
Recommended Posts:
- C++ | Exception Handling | Question 11
- C++ | Exception Handling | Question 1
- C++ | Exception Handling | Question 2
- C++ | Exception Handling | Question 3
- C++ | Exception Handling | Question 4
- C++ | Exception Handling | Question 5
- C++ | Exception Handling | Question 10
- C++ | Exception Handling | Question 9
- C++ | Exception Handling | Question 8
- C++ | Exception Handling | Question 7
- C++ | Exception Handling | Question 6
- Java | Exception Handling | Question 1
- Java | Exception Handling | Question 2
- Java | Exception Handling | Question 3
- Java | Exception Handling | Question 4



