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.
Join them; it only takes a minute:
|
|
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 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:
is the set of negative real numbers. So what is the point of the |
|||||||||||||||||
|
|
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 |
|||||
|
|
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. |
|||||
|
|
Pretty good explanation form cplusplus.com:
|
|||
|
|
|
It refers to mathematical domains.
|
|||
|
|
|
"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. |
|||
|
|
|
Well, this is all the guidance you get from the C++ standard:
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. |
|||
|
|