4.10.1 Unwind exceptions
Starting with SWI-Prolog 9.3.13, SWI-Prolog introduces a new class of
reserved exceptions. An exception of the shape unwind(Term)
is handled special by catch/3
and friends. Rather than simply calling the
Recover goal (third argument), catch/3
acts as if called as
catch(Goal, Ball, call_cleanup(once(Recover), throw(Ball)))
The above implies that cleanup that may be required on exceptions should use call_cleanup/2 (or one of its variations) or should perform the cleanup in the Recover goal. For example, the following does not properly cleanup after an exception:
( catch(Goal, Error, true) -> ( var(Error) -> <no exception> ; <cleanup> % NOT called on throw(unwind(...)) ...
This implies that, unless the user ensures Recover does not terminate, the exception will unwind the entire Prolog stack. While foreign code that catches these exceptions should propagate them to the parent environment, we cannot enforce this behaviour.
Currently, this mechanism defines these values for Term:
- abort
- Abort the current Prolog thread. This exception is raised by abort/0.
Previous versions raised
'$aborted'
using the same unwind semantics. - halt(Status)
- In the standard setup, if
unwind(
is raised in thehalt(Status)
)main
thread, this system halts with exit code Status - thread_exit(ExitValue)
- This exception is raised by thread_exit/1. See this predicate for details.