Previous: , Up: ref-ere-hex   [Contents][Index]


4.15.3.2 Handling Unknown Predicates

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)   hook

If 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

Causes calls to undefined predicates to be reported and the debugger to be entered at the earliest opportunity. Not available in runtime systems.

error   ISO

Causes calls to such predicates to raise an exception (the default).

warning   ISO

Causes calls to such predicates to display a warning message and then fail.

fail   ISO

Causes calls to such predicates to fail.

Finally, this flag can be accessed by the built-in predicate:

unknown(?OldValue, ?NewValue)   development

This 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.



Send feedback on this subject.