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
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, that is a type error. If an atom is given that is not a valid mode,
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, 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 it
would have passed the type test but would raise a domain error.
For example, the goal
open(somefile,rread,S)
raises the exception
domain_error(open(somefile,rread,_2490),2,'i/o mode',rread,'')
The Message argument is used to provide extra information about the problem.