Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences between out_of_range, range_error, and over/underflow_error?

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?
like image 528
Thanatos Avatar asked Apr 10 '14 04:04

Thanatos


People also ask

What is std :: Out_of_range?

Defined in header <stdexcept> class out_of_range; Defines a type of object to be thrown as exception. It reports errors that are consequence of attempt to access elements out of defined range.

How do you handle an out of range exception in C++?

To throw an out-of-range exception in C++, you can create an exception object. For this, you can use the out_of_range() constructor. The out_of_range() constructor is defined in the standard C++ library. It takes a string object as its input argument and returns an out-of-range exception.


2 Answers

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.

like image 96
user657267 Avatar answered Oct 12 '22 08:10

user657267


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.

like image 24
Escualo Avatar answered Oct 12 '22 09:10

Escualo