The catch predicate can catch (certain) exceptions generated by throw/1 or other built-in predicates, its prototype is:
catch(Goal, Catcher, RecoverGoal).
The first argument is the goal that is executed as if call(Goal) is executed. If an exception is thrown then the exception term is matched with the pattern Catcher, if the match is successful then the proving of RecoverGoal is started. If the match fails then throwing the exception continues. Exceptions generated during the proving of the RecoverGoal cannot be catched by the pattern of the same catch/3 clause.
see also: always/2 call/1 catch/2 fail/0 halt/0 halt/1 if-then/2 if-then-else/2 once/1 repeat/0 throw/1 true/0
| Examples | |
| catch(test1, Y, do_catched_exception(Y)). | executes the goal 'test1' and catches all exceptions that match with the empty variable Y, i.e. all exceptions thrown are catched. The exception is passed on as an argument to the clause 'do_catched_exception' |
| catch(do(A), badcommand(X), (recover(X), do2(A))) | executes the goal 'do(A)', only exceptions that match with 'badcommand(X)' are catched, if an exception is catched the expression (recover(X), do2(A)) becomes the new goal |
| Exceptions | |
| Goal argument is a empty variable | an instantiation_error is thrown |