Next: ref-ere-err-eva, Previous: ref-ere-err-typ, Up: ref-ere-err [Contents][Index]
A domain error occurs when an input argument is of the right type but
there is something wrong with its value. For example, the second argument to
open/[3,4]
is supposed to be an atom that represents a valid mode for opening
a file, such as read
or write
. If a number or a compound term is given
instead, then that is a type error. If an atom is given that is not a valid mode,
then that is a domain error.
The main reason that we distinguish between type errors and domain errors is that they usually represent different sorts of mistakes in your program. A type error usually indicates that you have passed the wrong argument to a command, whereas a domain error usually indicates that you passed the argument you meant to check, but you hadn’t checked it enough.
The SICStus_Error term associated with a domain error is
domain_error(Goal, ArgNo, DomainName, Culprit)
The arguments correspond to those of the SICStus_Error term for a type error,
except that DomainName is not in general the name of a unary
predicate: it needn’t even be an atom. For example, if some command
requires an argument to be an integer in the range 1..99, then it might use
between(1,99)
as the DomainName. With respect to the date_plus
example under Type Errors, if the month had been given as 13, then it
would have passed the type test but would throw a domain error.
For example, the goal
open(somefile,rread,S)
throws the exception
error(domain_error(io_mode,rread), domain_error(open(somefile,rread,_A),2,io_mode,rread))
The Message argument is used to provide extra information about the problem.