Users can write a handler for the specific exception occurring when an
undefined predicate is called by defining clauses for the hook
predicate user:unknown_predicate_handler/3
. This can be thought of as
a “global” exception handler for this particular exception, because
unlike catch/3
, its effect is not limited to a particular
goal. Furthermore, the exception is handled at the point where the
undefined predicate is called.
The handler can be written to apply to all unknown predicates, or to a class of them. The reference page contains an example of constraining the handler to certain predicates.
If call(
Module:Goal)
is the trapped call to the undefined
predicate, then the hook is called as:
user:unknown_predicate_handler(
+Goal,
+Module,
-NewGoal)
hookIf this succeeds, then Prolog replaces the call to the undefined
predicate with the call to Module:NewGoal. Otherwise, the action
taken is governed by the unknown
Prolog flag
(see ref-lps-flg), the allowed values of which are:
trace
error
ISOwarning
ISOfail
ISOFinally, this flag can be accessed by the built-in predicate:
unknown(
?OldValue,
?NewValue)
developmentThis unifies OldValue with the current value, sets the flag to NewValue, and prints a message about the new value. See mpg-ref-unknown_predicate_handler.