13.44 <stdexcept>

The <stdexcept> header defines 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
figs/cppn_1324.gif

See Chapter 3 for more information about throw expressions and Chapter 4 for information about handling exceptions with try statements.

domain_error class Arithmetic domain error

class domain_error : public logic_error {

public:

  explicit domain_error(const string& what_arg);

};

The domain_error class is used to report domain errors, that is, arguments to functions that are outside the valid domain for input to the functions. 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.

See Also

logic_error class

invalid_argument 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_argument for any other situations. For example, constructing a bitset from a string throws invalid_argument if any character is other than '0' or '1'.

See Also

logic_error class

length_error class Exceed maximum size

class length_error : public logic_error {

public:

  explicit length_error(const string& what_arg);

};

The length_error class is used to 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.

See Also

logic_error class

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.

See Also

domain_error class, invalid_argument class, length_error class, out_of_range class, runtime_error class

out_of_range class Argument out of range

class out_of_range : public logic_error {

public:

  explicit out_of_range(const string& what_arg);

};

The out_of_range class is used 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.

See Also

logic_error class

overflow_error class Arithmetic overflow

class overflow_error : public runtime_error {

public:

  explicit overflow_error(const string& what_arg);

};

The overflow_error class can be used for arithmetic overflow. For example, bitset::to_ulong throws overflow_error if the arithmetic value of the bitset exceeds the maximum value of an unsigned long.

figs/acorn.gif

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 this or any other exception.

See Also

runtime_error class

range_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 used 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 its return type.

See Also

runtime_error class

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.

See Also

overflow_error class, range_error class, underflow_error class

underflow_error class Arithmetic underflow

class underflow_error : public runtime_error {

public:

  explicit underflow_error(const string& what_arg);

};

figs/acorn.gif

The underflow_error class can be used for arithmetic underflow. Note that underflow in most arithmetic expressions has undefined behavior; an implementation might throw underflow_error, but there is no guarantee that it will throw this or any other exception.

See Also

runtime_error class