What will be the output of the following C++ code? [code] #include <iostream> using namespace std; int main() { try {

Tiên Hương

New member
What will be the output of the following C++ code?
Code:
#include <iostream>
using namespace std; 
int main()
{
    try
    {
        try
        {
            throw 20;
        }
        catch (int n)
        {
            cout << "Inner Catch\n";
            throw;
        }
    }
    catch (int x)
    {
        cout << "Outer Catch\n";
    }
    return 0;
}
A. Inner Catch
B. Outer Catch
C.
Inner Catch
Outer Catch
D. Error
 
Answer: c
Explanation: The exception thrown by the inner try catch block is caught by the inner block hence "Inner Catch" is printed but as inner catch block again throws an exception further therefore the exception is thrown further which is caught by the outer catch block hence "Outer Catch" is also printed.
 

Tìm kiếm

Back
Top