Home » C++ Programming » Exception Handling » Question
  1. What is the output of this program?
    #include <iostream>
    #include <string>
    using namespace std;
    int main ()
    {
    int n = 5;
    string str = "Exception Caught: Wrong number used";
    try
    {
    if ( n == 1 )
    {
    throw 10;
    }
    if ( n == 2 )
    {
    throw 3.5f;
    }
    if ( n != 1 || n != 2 )
    {
    throw str;
    }
    }
    catch (int p)
    {
    cout << "Exception value is: " << p << endl;
    }
    catch (float q)
    {
    cout << "Exception value is: " << q << endl;
    }
    catch (string str)
    {
    cout << str << endl;
    }
    return 0;
    }
    1. Exception value is: 10
    2. Compilation Error
    3. Exception value is: 3.5
    4. Exception Caught: Wrong number used
    5. None of these
Correct Option: D

As we are giving 5 to n, It is arising an exception named
“Exception Caught: Wrong number used”.



Your comments will be displayed only after manual approval.