[mercury-users] question about error/1

Peter Schachte schachte at csse.unimelb.edu.au
Thu Jan 24 16:18:04 AEDT 2008

Mark Brown wrote:
> I think we should export code like this from the exception module.  Eg:
> 	try_main(MyMain, MsgPred, !IO)
> where MyMain has a signature like main/2, and MsgPred accepts a string
> input and an io pair.  If MyMain throws an exception of the type generated
> by the "abort with message" predicate, then MsgPred is called with the
> given string.
> You use this like:
> 	main(!IO) :-
> 		try_main(main_2, io.write_string, !IO).
> 	:- pred main_2(io::di, io::uo) is det.
> 	main_2(!IO) :-
> 		...

This creates an unpleasant coupling between the main code for the application 
and the code that wants to abort.

How about making exceptions a type class with a method to return the 
exception message as a string?  Then make the usual exceptions include the 
"Uncaught exception" herald at the beginning of their message.  Then the 
usual main driver code could use that method to produce the string to print 
if main/2 terminates in an exception, but the new abort_exception type, also 
an instance of the exception type class, would have a to_string method that 
just passes back the string unchanged.  Then put a predicate like

	:- pred abort(string::in) is erroneous.

	abort(Message) :- throw(abort_exception(Message)).

in the library somewhere.  This gives everyone an easy way to abort, printing 
an unadorned message, by just calling abort/1 without having to write a 
special main/2 that anticipates it.

Peter Schachte              We should not focus on giving any one group an
schachte at cs.mu.OZ.AU        especially good life, but on giving the greatest
www.cs.mu.oz.au/~schachte/  possible life for the community as whole.
Phone: +61 3 8344 1338          -- Plato
mercury-users mailing list
Post messages to:       mercury-users at csse.unimelb.edu.au
Administrative Queries: owner-mercury-users at csse.unimelb.edu.au
Subscriptions:          mercury-users-request at csse.unimelb.edu.au

More information about the users mailing list