The exception class is a simple container for a C string: the string is passed as a constructor parameter and is available through the what accessor. The Standard Library declares the exception class in the <exception> library, and you are encouraged to derive your own exception classes from this. The Standard Library provides the following derived classes; most are defined in <stdexcept>.
Class | Thrown |
bad_alloc | When the new operator has been unable to allocate memory (in <new>) |
bad_array_new_length | When the new operator has been asked to create an array with an invalid length (in <new>) |
bad_cast | When dynamic_cast to a reference type fails (in <typeinfo>) |
bad_exception | An unexpected condition has occurred (in <exception>) |
bad_function_call | Invoked an empty function object (in <functional>) |
bad_typeid | When the argument of typeid is null (in <typeinfo>) |
bad_weak_ptr | When accessing a weak pointer, which refers to an already destroyed object (in <memory>) |
domain_error | When an attempt is made to perform an operation outside the domain on which the operation is defined |
invalid_argument | When an invalid value has been used for a parameter |
length_error | When an attempt has been made to exceed the length defined for an object |
logic_error | When there is a logic error, for example, class invariants or pre-conditions |
out_of_range | When an attempt has been made to access elements outside of the range defined for the object |
overflow_error | When a calculation results in a value bigger than the destination type |
range_error | When a calculation results in a value outside the range for the type |
runtime_error | When an error occurs outside the scope of the code |
system_error | Base class to wrap operating system errors (in <system_error>) |
underflow_error | When a calculation results in an underflow |
All the classes, mentioned in the preceding table, have a constructor that takes a const char* or a const string& parameter, in contrast to the exception class that takes a C string (hence the base class is constructed using the c_str method if the description is passed through a string object). There are no wide character versions, so if you want to construct an exception description from a wide character string you have to convert it. Also, note that the standard exception classes only have one constructor parameter, and this is available through the inherited what accessor.
There is no absolute rule about the data that an exception can hold. You can derive a class from exception and construct it with whatever values you want to make available to the exception handler.