what is a domain error

后端 未结 7 1429

in c++, 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

相关标签:
7条回答
  • 2021-02-05 19:48

    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 讨论(0)
  • 2021-02-05 19:54

    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.

    0 讨论(0)
  • 2021-02-05 19:55

    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.

    0 讨论(0)
  • 2021-02-05 20:00

    "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.

    0 讨论(0)
  • 2021-02-05 20:02

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

    0 讨论(0)
  • 2021-02-05 20:03

    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

    0 讨论(0)
提交回复
热议问题