Home » C++ Programming » Exception Handling » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class BaseClass
    {
    protected:
    int num;
    public:
    BaseClass()
    {
    num = 25;
    }
    BaseClass(int k)
    {
    num = k;
    }
    virtual ~BaseClass()
    {
    if (num < 0) throw num;
    }
    virtual int getA()
    {
    if (num < 0)
    {
    throw num;
    }
    }
    };
    int main()
    {
    try
    {
    BaseClass obj(-23);
    cout << endl << obj.getA();
    }
    catch (int)
    {
    cout << endl << "Illegal initialization";
    }
    }
    1. Runtime Error
    2. Compilation Error
    3. Illegal initialization
    4. Garbage value
    5. None of these
Correct Option: B

Compilation Error

main.cpp: In destructor ‘virtual BaseClass::~BaseClass()’:
main.cpp:18:33: warning: throw will always call terminate() [-Wterminate]
if (num < 0) throw num;
^~~
main.cpp:18:33: note: in C++11 destructors default to noexcept
main.cpp: At global scope:
main.cpp:39:5: fatal error: error writing to /tmp/cci28TfS.s: No space left on device
}
^
compilation terminated.



Your comments will be displayed only after manual approval.