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 mistake 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 exception code associated with a domain error is
domain_error(Goal, ArgNo, DomainName, Culprit, Message)
The arguments correspond to those of the exception code 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.
Some DomainNames recognized by the system include:
0
-- no domain name specified
atom
'atom or list'
atomic
between(
L,H)
-- 'something between L and H
built_in
callable
char
character
compiled
directory
declaration
db_reference
foreign
file
flag
imported_predicate
integer
interpreted
list
module
number
one_of(
List)
-- a member of the set List
pair
predicate_specification
procedure
stream
term
value(
X)
-- the value X
clause