4.10.3.1 General form of the ISO standard exception term
The predicate throw/1
takes a single argument, the exception term, and the ISO
standard stipulates that the exception term be of the form error(Formal,
Context)
with:
- Formal
theāformal' description of the error, as listed in chapter 7.12.2 pp. 62-63 ("Error classification") of the ISO standard. It indicates the error class and possibly relevant error context information. It may be a compound term of arity 1,2 or 3 - or simply an atom if there is no relevant error context information. - Context
additional context information beyond the one in Formal. If may be unset, i.e. a fresh variable, or set to something that hopefully will help the programmer in debugging. The structure of Context is left unspecified by the ISO Standard, so SWI-Prolog creates it own convention (see below).
Thus, constructing an error term and throwing it might take this form (although you would not use the illustrative explicit naming given here; instead composing the exception term directly in a one-liner):
Exception = error(Formal, Context), Context = ... some local convention ..., Formal = type_error(ValidType, Culprit), % for "type error" for example ValidType = integer, % valid atoms are listed in the ISO standard Culprit = ... some value ..., throw(Exception)
Note that the ISO standard formal term expresses what should be the case or what is the expected correct state, and not what is the problem. For example:
- If a variable is found to be uninstantiated but should be
instantiated, the error term is
instantiation_error
: The problem is not that there is an unwanted instantiation, but that the correct state is the one with an instantiated variable. - In case a variable is found to be instantiated but should be
uninstantiated (because it will be used for output), the error term is
uninstantiation_error(Culprit)
: The problem is not that there is lack of instantiation, but that the correct state is the one which Culprit (or one of its subterms) is more uninstantiated than is the case. - If you try to disassemble an empty list with compound_name_arguments/3,
the error term is
type_error(compound,[])
. The problem is not that
is (erroneously) a compound term, but that a compound term is expected and[]
does not belong to that class.[]