I’m using swi-prolog and trying to use exception handling. I’ve got a system like
writeAndFail(message)
:- write(message)
, fail.
pred(arg1, arg2)
:- catch(
real_pred(arg1, arg2),
Exception,
writeAndFail(Exception)
).
In the body of real_pred I throw a string directly, e.g. throw('message'). The call to pred (on a suitable test case) throws the exception and catches it correctly, but the message is never written out. How can I alter this code so that the exception message is printed?
You are using lowercase terms where it looks like you want variables.
The atom
messagein the predicatewriteAndFailis, and will always be the literal atommessage. It will not unify with anything else other than the literal atommessage.Calling
writeAndFail(hello)will fail immediately because there is no predicate that matches.The same is true for
arg1,arg2inpred.If you call
predwith literal arguments,arg1, andarg2and if throw really throws a literal'message'then those values should unify and you should see the expected results.Otherwise, start your variables with an uppercase letter.