Exception Handling


  1. What is the output of this program?
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    double Option1 = 12, Option2 = 6, Result;
    char Option;
    try
    {
    if (Option != '+' && Option != '-' && Option != '*' && Option != '/')
    throw Option;
    switch(Option)
    {
    case '+':
    Result = Option1 + Option2;
    break;
    case '-':
    Result = Option1 - Option2;
    break;
    case '*':
    Result = Option1 * Option2;
    break;
    case '/':
    Result = Option1 / Option2;
    break;
    }
    cout << "\n" << Option1 << " " << Option << " "<< Option2 << " = " << Result;
    }
    catch (const char ch)
    {
    cout << ch << "Is not a valid Option...";
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    It will arise a exception because we missed a operator.


  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    double DivFunction(int num1, int num2)
    {
    if ( num2 == 0 )
    {
    throw "Division by zero condition Occurred...";
    }
    return (num1 / num2);
    }
    int main ()
    {
    int m = 10;
    int n = 0;
    double Res = 0;
    try
    {
    Res = DivFunction(m, n);
    cout << Res << endl;
    }
    catch (const char* msg)
    {
    cout << msg << endl;
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    We are dividing the values and if one of the values is zero means, We are arising an exception.



  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    int main()
    {
    int a = 8;
    try
    {
    if (a < 0)
    throw "Positive Number Required";
    cout << a << "\n\n";
    }
    catch(const char* Msg)
    {
    cout << "Error: " << Msg;
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    In this program, We are checking the age of a person, If it is zero means, We will arise a exception.


  1. What is the output of this program?
    #include <iostream>
    #include <exception>
    using namespace std;
    class ExceptionExample: public exception
    {
    virtual const char* what() const throw()
    {
    return "Exception Occurred...";
    }
    } NewExcep;
    int main ()
    {
    try
    {
    throw NewExcep;
    }
    catch (exception& excep)
    {
    cout << excep.what() << endl;
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    In this program, We are arising a standard exception and catching that and returning a statement.



  1. Which is used to check the error in the block?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    The try block is used to check for errors, if there is any error means, it can throw it to catch block.