Could someone explain what the differences between range_error
, out_of_range
, and the pair of overflow_error
and underflow_error
are, when I should use each? They seem all like the same thing.
According to cppreference.com:
out_of_range
: It reports errors that are consequence of attempt to access elements out of defined range.range_error
: It reports errors that arise because floating point value in some computation could not be represented because it was too large or too small in magnitude. If the value has integral type,std::underflow_error
orstd::overflow_error
should be used.overflow_error
: It reports errors that arise because integer value in some computation could not be represented as it had too large positive value.
Specifically, I have a function,
template<typename T>
void write_integer(const T &n) {
if(n < 0) { throw ??? }
if(some_limit < n) { throw ??? }
Where T
is an integral type; the function does some bounds-checking on n
, to see if it's within a certain range; if it isn't, I'd like to throw some exception. I'm confused because:
out_of_range
sounds like it's for indexing and array-bounds checking, which I'm not doing.range_error
appears to be for floats? (But why, in a language like C++?)underflow_error
andoverflow_error
? Are these really appropriate?