11.3.31 call_cleanup/2

call_cleanup(+Goal, +Cleanup)


Executes the procedure call Goal. When Goal succeeds determinately, is cut, fails, or raises an exception, Cleanup is executed.


callable, must be nonvar
callable, must be nonvar


This construction can be used to ensure that Cleanup is executed as soon as Goal has completed execution, no matter how it finishes. In more detail:

When call_cleanup/2 with a continuation C is called or backtracked into, first Goal is called or backtracked into. Then there are four possibilities:

  1. Goal succeeds determinately, possibly leaving some blocked subgoals. Cleanup is executed with continuation C.
  2. Goal succeeds with some alternatives outstanding. Execution proceeds to C. If a cut that removes the outstanding alternatives is encountered, Cleanup is executed with continuation to proceed after the cut. Also, if an exception E that will be caught by an ancestor of the call_cleanup/2 Goal is raised, Cleanup is executed with continuation raise_exception(E).
  3. Goal fails. Cleanup is executed with continuation fail.
  4. Goal raises an exception E. Cleanup is executed with continuation raise_exception(E).

In a typical use of call_cleanup/2, Cleanup succeeds determinately after performing some side-effect; otherwise, unexpected behavior may result.

Note that the Prolog top-level operates as a read-execute-fail loop, which backtracks into or cuts the query when the user types ; or <RET> respectively. Also, some predicates, such as halt/[0,1] and abort/0, are implemented in terms of exceptions. All of these circumstances can trigger the execution of Cleanup.


Depends on the arguments.


Call errors (see ref-sem-exc).

See Also


Send feedback on this subject.