Home » C++ Programming » Exception Handling » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    int main()
    {
    int num = -2;
    try
    {
    if (num < 0)
    {
    throw num;
    }
    else
    {
    cout<< num;
    }
    }
    catch (int num )
    {
    cout << "Exception occurred: Thrown value is " << num << endl;
    }
    return 0;
    }
    1. Compilation Error
    2. Runtime Error
    3. -2
    4. Exception occurred: Thrown value is -2
    5. None of these
Correct Option: D

As the given value is -2 and according to the condition, We are arising an exception.



Your comments will be displayed only after manual approval.