17

in c++, <stdexcept> has a base class for 'domain errors', std::domain_error. i don't understand under what circumstances i should throw a domain error in my code. all of the other exception base classes are pretty self explanatory. i'm pretty sure that std::domain_error has nothing to do with internet domain names, per se, so please explain what class of error a domain error is and provide some examples.

2
  • so i'm seeing lots of responses about mathematical domains, e.g. sqrt(-1) should throw a domain_error (unless of course you implement complex numbers). how is this different from range_error? before your answers, i would have considered sqrt(-1) to be a range_error.
    – rev
    Mar 13, 2009 at 1:10
  • It should be noted that no c++ standard library function throws a domain error. It is intended for the end user.
    – Dan
    May 7, 2014 at 19:42

7 Answers 7

16

Domain and range errors are both used when dealing with mathematical functions.

On the one hand, the domain of a function is the set of values that can be accepted by the function. For example, the domain of the root square function is the set of positive real numbers. Therefore, a domain_error exception is to be thrown when the arguments of a function are not contained in its domain

On the other hand, the range of a function is the set of values that the function can return. For example, the range of a function like this one:

f(x) = -x²

is the set of negative real numbers. So what is the point of the range_error? If the arguments of the function are in its domain, then the result must be in its range, so we shouldn't have any errors wrt the range...However, sometimes the value can be defined, but without being representable. For example, in C, the functions in <math.h> generate errors if the return value is too large (or too small) in magnitude to represent

4
  • I wonder if it is standard or reprehensible to throw domain_error and range_error in non-mathematical functions, to represent the same error conditions (argument_error rather than domain_error perhaps?)
    – fuzzyTew
    Aug 26, 2011 at 14:56
  • 1
    @fuzzyTew: As a matter of fact, the standard library contains a class invalid_argument, so you should probably use that instead :)! Aug 26, 2011 at 18:45
  • Thanks for a good explanation of when to use the exception. Your view on range_error is also interesting. As cpplus.com states it's for "range errors in internal computations". It is even placed in runtime errors group...
    – Arks
    Feb 11, 2015 at 7:52
  • ...And the return value being too large or too small to be representable by the machine is exactly a runtime error. With the same way of thinking I can also see this exception being used when a function makes step by step calculations and even while both function domain and function ranges are ok, it still can get out of range somewhere in the middle.
    – Arks
    Feb 11, 2015 at 7:53
6

A domain error refers to issues with mathematical domains of functions. Functions are sometimes defined only for certain values. If you try to invoke such a function with an argument that is not part of its domain, that is a domain error.

For example, trying to invoke sqrt() with a negative argument is a domain error, since negative numbers are not part of the domain of sqrt().

1
  • 1
    But when I do sqrt(-1) it returns nan and doesn't throw an exception.
    – rmp251
    Nov 25, 2013 at 19:53
3

Pretty good explanation form cplusplus.com:

Generally, the domain of a mathematical function is the subset of values that it is defined for. For example, the square root function is only defined for non-negative numbers. Thus, a negative number for such a function would be a domain error.

3

Well, this is all the guidance you get from the C++ standard:

The class domain_error defines the type of objects thrown as exceptions by the implementation to report domain errors.

Domain here means "problem domain", nothing to do with the internet. For example, a square root function might throw a domain error if passed a negative number.

2

It's for logic errors of the class of "domain" errors. This could apply to any situation where the input to a function exceeds the allowable domain for it to operate on. That's its stated purpose in the standard.

For example, you have a function that takes only positive floats, so it throws a domain_error for negative numbers.

0
2

"Detailed Description Thrown by the library, or by you, to report domain errors (domain in the mathmatical sense). "

From: http://www.aoc.nrao.edu/~tjuerges/ALMA/STL/html/classstd_1_1domain__error.html

according to this, it should be used if you are given input that does not comply with the constraints you put on your interface. Like say, a function that is supposed to receive a positive value and is given a negative one.

1

It refers to mathematical domains.

float MySqrRoot(float x)
{
    // sqrt is not valid for negative numbers.
    if (x < 0) throw new domain_error;

    //...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.