Exception can be of only built-In type - Exception Handling

Q.  An exception can be of only built-In type.
- Published on 19 Oct 15

a. True
b. False

ANSWER: False
 

    Discussion

  • Raj   -Posted on 12 Oct 15
    An exception can be of only built-In type is false statement.
    You can create user-defined exceptions. C++ provides a base class for all exception named as Exception. If you want to create your own exception then you have to inherit this class.
    class myException : public exception{

    public:

    const char * what() const throw()
    {

    return "Divide By Zero Exception occured\n";
    }
    };

    int main()

    {
    try
    {
    int a, b;
    cout << "Enter two numbers : ";
    cin >> a >> b;
    if (b == 0)

    {
    myException exp;

    throw exp;
    }
    else
    {

    cout << "a / b = " << a/b << endl;
    }

    }
    catch(exception& eObj)

    {

    cout << eObj.what();

    }

    }

    Exception class provide what() public method and it should be overridden by all the child exception classes.

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)