Differences between out_of_range, range_error, and over/underflow_error?
Asked Answered
B

2

14

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 or std::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 and overflow_error? Are these really appropriate?
Bullnecked answered 10/4, 2014 at 4:48 Comment(1)
Out-of-range = input value makes no sense. All the rest = output value or intermediate value cannot be represented. You may want to consider std::domain_error.Hammertoe
L
12

From the standard:

range_error: The class range_error defines the type of objects thrown as exceptions to report range errors in internal computations.

out_of_range: The class out_of_range defines the type of objects thrown as exceptions to report an argument value not in its expected range.

overflow_error: The class overflow_error defines the type of objects thrown as exceptions to report an arithmetic overflow error.

underflow_error: The class underflow_error defines the type of objects thrown as exceptions to report an arithmetic underflow error.

For your function the most appropriate would be out_of_range.

Lynnette answered 10/4, 2014 at 4:54 Comment(0)
M
5

I have not thought a lot about it, but my guidelines seem to be:

  • out_of_range when my abstraction can be thought of as a series of buckets or pigeonholes (discrete), and I requested a non-existing bucket or pigeonhole. The chief example being requesting the fourth element of a three-element array (the slots in the array can be thought of as buckets).

  • range_error when the requested operation does not make mathematical sense in the domain considered (for example, the square root of a negative number when dealing with a real domain).

  • overflow_error and underflow_error when the result would exceed the capacity of the underlying type. For example: an integer number larger than what can be held my int.

Mccreery answered 10/4, 2014 at 5:0 Comment(1)
I found your examples more helpful than the marked answer, especially in the case of range_errorFawnia

© 2022 - 2024 — McMap. All rights reserved.