Exception Handling


  1. What is the output of this program?
    #include <iostream>
    #include <exception>
    #include <typeinfo>
    using namespace std;
    class Sample
    {
    virtual int Function()
    {
    }
    };
    int main ()
    {
    try
    {
    Sample *ptr = NULL;
    typeid (*ptr);
    }
    catch (std::exception& TypeVar)
    {
    cout << "Exception Occurred: " << TypeVar.what() << endl;
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    As we are using a bad type on pointers, So it is arising an error.


  1. What is the output of this program?
    #include <iostream>
    #include <string>
    #include <typeinfo>
    using namespace std;
    int main( )
    {
    try
    {
    string str1("Interview");
    string str2("Mania");
    str1.append(str2, 6, 3);
    cout << str1 << " " << endl;
    }
    catch (exception &e)
    {
    cout << "Caught: " << e.what() << endl;
    cout << "Type: " << typeid(e).name() << endl;
    };
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    out_of_range



  1. What is the output of this program?
    #include <typeinfo>
    #include <iostream>
    using namespace std;
    class AngleShape
    {
    public:
    virtual void myvirtualfunc() const {}
    };
    class Triangle: public AngleShape
    {
    public:
    virtual void myvirtualfunc() const
    {
    };
    };
    int main()
    {
    AngleShape Angleshape_instance;
    AngleShape &ref_Angleshape = Angleshape_instance;
    try
    {
    Triangle &ref_Triangle = dynamic_cast <Triangle& > (ref_Angleshape);
    }
    catch (bad_cast)
    {
    cout << "Can't do the Dynamic_cast" << endl;
    cout << "Caught: bad_cast exception. AngleShape is not Triangle.\n";
    }
    return 0;
    }











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    As we can’t able to create the dynamic instance for the triangle, So it is arising an exception.


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    The size of unsigned long int always depends on compiler.



  1. What do you mean by “No exception specification”?











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    It can throw anything