
error.pl -- Error generating support
This module provides predicates to simplify error generation and checking. It's implementation is based on a discussion on the SWI-Prolog mailinglist on best practices in error handling. The utility predicate must_be/2 provides simple run-time type validation. The *_error predicates are simple wrappers around throw/1 to simplify throwing the most common ISO error terms.
type_error(+ValidType, +Culprit)
- Tell the user that Culprit is not of the expected ValidType. This
error is closely related to domain_error/2 because the notion of
types is not really set in stone in Prolog. We introduce the
difference using a simple example.
Suppose an argument must be a non-negative integer. If the actual argument is not an integer, this is a type_error. If it is a negative integer, it is a domain_error.
Typical borderline cases are predicates accepting a compound term, e.g.,
point(X,Y)
. One could argue that the basic type is a compound-term and any other compound term is a domain error. Most Prolog programmers consider each compound as a type and would consider a compound that is notpoint(_,_)
a type_error.
Undocumented predicates
The following predicates are exported, but not or incorrectly documented.
uninstantiation_error(Arg1)
resource_error(Arg1)
permission_error(Arg1, Arg2, Arg3)
must_be(Arg1, Arg2)
is_of_type(Arg1, Arg2)
instantiation_error(Arg1)
representation_error(Arg1)
current_type(Arg1, Arg2, Arg3)
existence_error(Arg1, Arg2, Arg3)
existence_error(Arg1, Arg2)
domain_error(Arg1, Arg2)
syntax_error(Arg1)