Reputation: 3434
In order to run a group of queries in a simple query, I have tagged these queries and use forall/2
to call them:
query_all :-
forall(query(Q), (Q ->
format('yes: ~w~n',[Q]) ;
format('no : ~w~n',[Q]))).
so if I define something like query(true).
, I'll be able to see yes: true
from the output.
The problem here is that query( ... )
do not always exist, when prolog can't find anything that tagged query
, forall/2
will fail and cause exception saying "error(existence_error(procedure,query/1),forall/2)"
I want to handle this exception, but not to break the whole control flow.
I know catch/3 would help me but I don't know how to use it, my code is:
catch(query_all, error(existence_error(procedure,_),_), recovery).
recovery :-
format('error occurred.~n',[]).
but prolog says "native code procedure catch/3 cannot be redefined". Is there something I've missed?
Upvotes: 0
Views: 511
Reputation: 22585
You can either declare query/1
as dynamic in your code adding this line:
:-dynamic(query/1).
or use catch/3
as you suggested, however you don't have to redefine it but use it instead, e.g:
query_all :-
catch(
forall(query(Q), (Q ->
format('yes: ~w~n',[Q]) ;
format('no : ~w~n',[Q]))),
error(existence_error(procedure, _), _), format('error occurred.~n', [])).
Upvotes: 3