% For Prova 1.7.1 or later.
% NOTE that exception traces are intentionally printed in this example. 
% This resumes the example ex009.prova 
% Demonstrate
% - that consult is atomic,
% - how parsing errors can be caught by multiple exception handlers.

:- eval(ex012()).

filename("nosuchfile.txt").
filename("ex009_include.prova").

ex012() :-
	filename(Filename),
	on_exception(java.io.IOException,on_io_errors(Filename)),
	on_exception(ws.prova.parser.ParsingException,on_parsing_errors(Filename)),
	ex012_inner(Filename),
	consult(Filename),
	println(["***** This is printed once: ",Filename]),
	% Run the goal from the consulted file:
	%    will not be reached as either there is no such file or there are errors.
	ex009_include().

ex012_inner(Filename) :-
	% This handler points to a non-existing predicate but it
	%    will not be used anyway in the calling clause ex012() 
	on_exception(ws.prova.parser.ParsingException,on_parsing_errors2(Filename)).

% Note that the following handler fails, so consult fails also,
%    and backtracking to the filename(Filename) call occurs.
%    resumed after consult.
on_io_errors(Filename) :-
	exception(Ex),
	println([Ex]),
	fail().

% Note that the following handler does not fail, so the processing is
%    resumed after consult. 
on_parsing_errors(Filename) :-
	exception(Ex),
	println([Ex]).
