C++: Exceptions

throw anything

The throw statement will throw a value of any type, but if your exceptional condition fits into one of the standard exceptions, use that.

Standard exceptions

The C++ library defines a number of common exceptions. Use
    #include <stdexcept>
to get the following classes defined. These are arranged in a class hierarchy shown by the indentation below.

Standard exception constructor

To create one of the exceptions, you need to call the constructor, which takes a string.
   throw out_of_range("Subscript less than zero");

Catching a standard exception

You can catch an exception, or any of its subclass. Use the what() method to get the error string from a standard exception.
try {
    . . .
    x = v[-2];
    . . .
} catch (out_of_range e) {
    cerr << e.what() << endl;
    exit(1);
}