Home » C++ Programming » Classes & Objects » Question
  1. What is the output of this program?
    #include <iostream>
    using namespace std;
    class ExceptionClass
    {
    public:
    ExceptionClass(int value) : mValue(value)
    {
    }
    int mValue;
    };
    class DerivedExceptionClass : public ExceptionClass
    {
    public:
    DerivedExceptionClass(int value, int anotherValue) : ExceptionClass(value),mAnotherValue(anotherValue)
    {
    }
    int mValue;
    int mAnotherValue;
    };
    void doSomething()
    {
    throw DerivedExceptionClass(10,20);
    }
    int main()
    {
    try
    {
    doSomething();
    }
    catch (DerivedExceptionClass &exception)
    {
    cout << "Caught Derived Class Exception";
    }
    catch (ExceptionClass &exception)
    {
    cout << "Caught Base Class Exception";
    }
    return 0;
    }
    1. Caught Base Class Exception
    2. Compilation Error
    3. Caught Derived Class Exception
    4. Runtime Error
    5. None of these
Correct Option: C

As we are throwing the value from the derived class, it is arising an exception in derived class



Your comments will be displayed only after manual approval.