Increase error in SWI Prolog

I want to print a message and stop evaluating the predicate. How to do it?

+4
source share
2 answers

Take a look at the link that describes the catch / 3 and throw / 1 mechanisms in Prolog.

Using this mechanism, an exception can be thrown or an exception handled.

Example (given on the site):

p:- true. p:- throw(b). q:- catch(p, B, write('hellop')), r(c). r(X) :- throw(X). 

Then call:

  ?- catch(p, X, (write('error from p'), nl)). 

will illustrate the handling of ecxeption.

+2
source

I played with several other examples that I found. This may be helpful.

 p :- throw(b). r(X) :- throw(X). q:- catch(p, B, format('Output1: Error from throwing p')), r(B). catch(throw(exit(1)), exit(X), format('Output: `w', [X])). Output: 1 1 is thrown to catcher 'exit(X)' and recovered by format('Output: ~w', [X])), catch(p, C, format('hellop')). Output: hellop p throws 'b' which is recovered by writing 'hellop' catch(q, C, format('Output2, Recovering q: helloq ')). Output1: Error from throwing p Output2, Recovering q: helloq 

Ben

+1
source

Source: https://habr.com/ru/post/1346049/


All Articles