tags:

views:

642

answers:

7

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.

+6  A: 

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().

John Feminella
+1  A: 

It refers to mathematical domains.

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

    //...
}
Michael
+2  A: 

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.

greyfade
how is that different than range_error?
rev
+2  A: 

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.

sth
+1  A: 

"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  A: 

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.

anon
+4  A: 

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 (cf. this page).

Luc Touraille