% The example demonstrates more features of exception handling in Prova.
%
% The method name 'ToString' starts with a capital letter and is interpreted by the parser as a variable.
% If the variable is free at the time of the invocation, in Prova version 1.8, an InferenceException is raised.
% In the following example, the exception is intercepted by a handler 'handle_exception'. As the handler receives
% the variable 'Text' that is supposed to be initialised by the offending code, it has an option to fail, in which case
% the offending code will also fail, or succeed and also instantiate whatever variables were passed to the handler,
% in this case, the variable 'Text'. This handler, called a compensation handler, will allow the original code to continue
% as if nothing had happened with the string literal 'complensation' assigned to variable 'Text' and printed at the end
% of clause ex027 normally.  

:- eval(ex027()).

ex027() :-
	on_exception(java.lang.Exception, handle_exception(Text)), 
	I = Integer(5),
	% If the line below is uncommented, no exception occurs
%	ToString = toString,
	Text = I.ToString(),
	println([Text]).

handle_exception(Text) :- 
	exception(Ex), 
	println([Ex]),
	Text = 'compensation'.
