Exception Handling


  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    void Example(int num)
    {
    try
    {
    if (num > 0)
    throw num;
    else
    throw 'U';
    }
    catch(int num)
    {
    cout<<"Integer: "<< num << endl;
    }
    catch(char ch)
    {
    cout << "Character: " << ch;
    }
    }
    int main()
    {
    Example(12);
    Example(0);
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    We are passing the integer and character and catching it by using multiple catch statement.


  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    int main()
    {
    int n1 = 20, n2 = 30, n3 = 60;
    float Res;
    try
    {
    if ((n1 - n2) != 0)
    {
    Res = n3 / (n1 - n2);
    cout << Res;
    }
    else
    {
    throw(n1 - n2);
    }
    }
    catch (int k)
    {
    cout<<"Answer is infinite..."<< k;
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    We are manipulating the values, if there is any infinite value means, it will raise an exception.



  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    int main()
    {
    try
    {
    throw 13;
    }
    catch (int ex)
    {
    cout << "Exception number: " << ex << endl;
    return 0;
    }
    cout << "No any Exception..." << endl;
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    If we caught a integer value means, there will be an exception, if it is not a integer, there will not be a exception.


  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    int main()
    {
    double num1 = 9, num2 = 3, Result;
    char Operator = '/';
    try
    {
    if (num2 == 0)
    throw "Division by zero not allowed";
    Result = num1 / num2;
    cout << num1 << " / " << num2 << " = " << Result;
    }
    catch(const char* S)
    {
    cout << "\n Bad Operator: " << S;
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    In this program, We are dividing the two variables and printing the result. If any one of the operator is zero means, it will arise a exception.



  1. What is the output of this program?
    #include <iostream>
    #include <exception>
    using namespace std;
    struct NewException : public exception
    {
    const char * what () const throw ()
    {
    return "Interview Mania";
    }
    };
    int main()
    {
    try
    {
    throw NewException();
    }
    catch(NewException& e)
    {
    cout << "Exception Caught..." << std::endl;
    cout << e.what() << std::endl;
    }
    catch(std::exception& e)
    {
    }
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    We are defining the user-defined exception in this program.