O'Reilly logo

Beginning C++ Programming by Richard Grimes

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

C++ exception syntax

In C++, an exceptional situation is generated by throwing an exception object. That exception object can be anything you like: an object, a pointer, or a built-in type, but because exceptions may be handled by code written by other people it is best to standardize the objects that are used to represent exceptions. For this, the Standard Library provides the exception class, which can be used as a base class.

    double reciprocal(double d)     {         if (d == 0)          {             // throw 0;             // throw "divide by zero";             // throw new exception("divide by zero");             throw exception("divide by zero");         }         return 1.0 / d;     }

This code tests the parameter and if it is zero then it throws an exception. Four examples are given and all are valid C++, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required