The <stdexcept>
header define several standard exception classes. Refer to <exception>
for the base exception class. Figure 13-24 shows all the exception classes in the standard library, including a few that are declared in other headers. Note that the standard library has very few places that throw exceptions. The exceptions in <stdexcept>
are available primarily for your use.
Figure 13-24: All the standard exception classes
Arithmetic domain error
class domain_error : public logic_error { public: explicit domain_error(const string& what_arg); };
The domain_error
class is thrown to report domain errors, that is, arguments to functions that are outside the valid domain for input to the function. For example, a function that converts a color from the Hue, Saturation, Lightness colorspace to the Red, Green, Blue colorspace might require a Saturation in the range [0.0, 1.0], and throw domain_error for any other value.
logic_error class
Invalid function argument
class invalid_argument : public logic_error { public: explicit invalid_argument(const string& what_arg); };
The invalid_
argument class is thrown to report invalid arguments to functions. Specific kinds of invalid arguments are covered by the other logic errors; use invalid_
a
rgument
for any other situations. For example, constructing a bitset
from a string
throws invalid_argument
if any character is other than '0'
or '1'
.
logic_error class
Exceed maximum size
class length_error : public logic_error { public: explicit length_error(const string& what_arg); };
The length_error
class is thrown for an attempt to set or change the size of an object that exceeds the maximum size. For example, the string
class throws length_error
if you attempt to create a string longer than max
_
size()
characters.
logic_error class
Base class for logic errors
class logic_error : public exception { public: explicit logic_error(const string& what_arg); };
The logic_error
class is a base class for logic error exceptions. A logic error is a violation of the preconditions or other requirements for a function.
domain_error class, invalid_argument class, length_error class, out_of_range class, runtime_error class
Argument out of range
class out_of_range : public logic_error { public: explicit out_of_range(const string& what_arg); };
The ou
t
_of_
r
ange
class is thrown when an index or similar value is out of its expected or allowed range. For example, the at
member (of deque
, string
, and vector
) throws out_of_range
if the index is invalid.
logic_error class
Arithmetic overflow
class overflow_error : public runtime_error { public: explicit overflow_error(const string& what_arg); };
The overflow_error
class can be thrown for arithmetic overflow. For example, bitset::to_
u
long
throws overflow_
e
rror
if the arithmetic value of the bitset
exceeds the maximum value of an unsigned
long
. Note that overflow in most arithmetic expressions has undefined behavior; an implementation might throw overflow_error
, but there is no guarantee that it will throw that or any other exception.
runtime_error class
Arithmetic range error
class range_error : public runtime_error { public: explicit range_error(const string& what_arg); };
The range_error
class can be thrown when a function's results would fall outside its valid range. Note that the <cmath>
functions do not throw any exceptions, but a third-party math library might throw range_error
for, say, computing a power that exceeds the limits of long
double
.
runtime_error class
Base class for runtime errors
class runtime_error : public exception { public: explicit runtime_error(const string& what_arg); };
The runtime_error class is the base class for runtime errors, which are errors that cannot reasonably be detected by a static analysis of the code, but can be revealed only at runtime.
overflow_error class, range_error class, underflow_error class
Arithmetic underflow
class underflow_error : public runtime_error { public: explicit underflow_error(const string& what_arg); };
The underflow_error
class can be thrown for arithmetic underflow. Note that overflow in most arithmetic expressions has undefined behavior; an implementation might throw underflow_error
, but there is no guarantee that it will throw that or any other exception.
runtime_error class