Exception Handling


  1. What is the output of this program?
    #include <iostream>
    #include <exception>
    using namespace std;
    class NewExcep: public exception
    {
    virtual const char* what() const throw()
    {
    return "New 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

    This is a type of exception arising in the class. We can call this
    also as a standard exception.


  1. What is the output of this program?
    #include <iostream>
    #include <exception>
    using namespace std;
    int main ()
    {
    try
    {
    float* myarray= new float[125];
    cout << "Memory Allocated";
    }
    catch (exception& excep)
    {
    cout << "Standard exception: " << excep.what() << endl;
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    Variable will be allocated depends on the available space in the memory, If there is no space means, It will throw an exception.



  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    int main()
    {
    char* p;
    unsigned long int num = (size_t(0) / 3);
    cout << num << endl;
    try
    {
    p = new char[size_t(0) / 3];
    delete[ ] p;
    }
    catch(bad_alloc &TheBadAllocation)
    {
    cout << TheBadAllocation.what() << endl;
    };
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    As we are dividing the zero by three, it is returning 0.


  1. What is the output of this program?
    #include <iostream>
    #include <typeinfo>
    using namespace std;
    class shapeClass
    {
    public:
    virtual void Virtualfunc() const {}
    };
    class NewTriangle: public shapeClass
    {
    public:
    virtual void Virtualfunc() const
    {
    };
    };
    int main()
    {
    shapeClass shape_object;
    shapeClass &Ref_shape = shape_object;
    try
    {
    NewTriangle &Ref_NewTriangle = dynamic_cast<NewTriangle&>(Ref_shape);
    }
    catch (bad_cast)
    {
    cout << "Exception caught by bad_cast";
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    As we are not able to allocate the values by using dynamic cast,
    So it is arising an exception.



  1. What will happen when an exception is uncaught?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    arise an error