C++ stdexcept underflow_error Class
Example
Throw an underflow_error when the result of multiplying two tiny numbers is too small for a double:
double a = 1e-200;
double b = 1e-200;
try {
if (a < DBL_MIN / b) {
throw underflow_error("The result is too small for a double");
}
cout << a * b;
} catch (const underflow_error& e) {
cout << e.what();
}
Definition and Usage
An underflow_error reports that the result of a calculation is too small for its data type. This happens with decimal numbers, when a result is so close to 0 that the data type cannot store it correctly.
1e-200 is a very tiny number (0.000...0001, with 200 decimal places). DBL_MIN (from <cfloat>) is the smallest positive double with full precision. The program checks if a < DBL_MIN / b, which means that a * b would be smaller than DBL_MIN.
The underflow_error class is defined in the <stdexcept> header file.
Tip: For a result that is too big, use overflow_error.
Syntax
throw underflow_error(message);
Catch it with catch (const underflow_error& e), and use e.what() to get the message.
Parameter Values
| Parameter | Description |
|---|---|
| message | Required. A text that describes the error, as a string or as text in double quotes. what() returns this text. |
Technical Details
| Returns: | An underflow_error object. Its what() function returns the message. |
|---|
Related Pages
Read more about exceptions in our Exceptions Tutorial.