Home Contents Index Summary Previous Next

B.3 Hooks using the exception/3 predicate

This section describes the predicate exception/3, which may be defined by the user in the module user as a multifile predicate. Unlike the name suggests, this is actually a hook predicate. Exceptions are handled by the ISO predicates catch/3 and throw/1. They all frames created after the matching catch/3 to be discarded immediately.

The predicate exception/3 is called by the kernel on a couple of events, allowing the user to alter the behaviour on some predefined events.

exception(+Exception, +Context, -Action)
Dynamic predicate, normally not defined. Called by the Prolog system on run-time exceptions that can be repaired `just in time'. Currently exception/3 is only used for trapping undefined predicates. The values for Exception are described below. See also catch/3 and throw/1.

undefined_predicate
If Exception is undefined_predicate Context is instantiated to a term Name/Arity. Name refers to the name and Arity to the arity of the undefined predicate. If the definition module of the predicate is not user, Context will be of the form <Module>:<Name>/<Arity>. If the predicate fails Prolog will generate an esistence_error exception. If the predicate succeeds it should instantiate the last argument either to the atom fail to tell Prolog to fail the predicate, the atom retry to tell Prolog to retry the predicate or error to make the system generate an exception. The action retry only makes sense if the exception handler has defined the predicate.