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++, ...