[mercury-users] question about error/1

Mark Brown mark at csse.unimelb.edu.au
Thu Jan 24 14:39:57 AEDT 2008


On 24-Jan-2008, Ralph Becket <rafe at csse.unimelb.edu.au> wrote:
> Mark Brown, Thursday, 24 January 2008:
> > Hi Nick,
> > 
> > On 24-Jan-2008, Nicholas Nethercote <njn at csse.unimelb.edu.au> wrote:
> > > On Thu, 24 Jan 2008, Ralph Becket wrote:
> > >
> > >>> When I call error/1, the program terminates with a message like this:
> > >>>
> > >>>   Uncaught Mercury exception:
> > >>>   Software Error: <message>
> > >>>
> > >>> Is it possible to omit the "Uncaught Mercury exception: Software Error:"
> > >>> bit?
> > >>
> > >> That's rather the point of error/1.  You just use throw/1 for other
> > >> exceptions.
> > >
> > > I just want to abort easily with a message of my choosing.  Instead I get a 
> > > message of my choosing plus some other stuff, which will likely confuse 
> > > users.  To avoid that confusion, I have to use 'try' + I/O, which is a 
> > > hassle.
> > >
> > > In short, it would be nice to be able to abort with a message of my 
> > > choosing.
> > 
> > The implementation of error/1 is
> > 
> >     error(Message) :-
> >         throw(software_error(Message)).
> > 
> > so I think throw/1 is what you want.

Wait, Julien and Ralph are right.  I forgot about the "Uncaught exception:"
part.

> 
> Talking to Nick, I think what he wants is something like
> abort_with_my_error_message_and_no_window_dressing(string).
> 
> You can code this up fairly easily at the top-level:
> 
> :- pred main(io::di, io::uo) is cc_multi.
> 
> main(!IO) :-
> 	try_io(my_main, Result, !IO),
> 	(
> 		Result = succeeded(_)
> 	;
> 		Result = exception(Excn),
> 		( if Excn = univ.value(no_window_dressing(ErrMsg)) then
> 			io.write_string(io.stderr_stream, ErrMsg, !IO),
> 			io.nl(io.stderr_stream, !IO),
> 			io.set_exit_status(1, 1IO)
> 		  else
> 		  	rethrow(Result)
> 		)
> 	).

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) :-
		...

Furthermore, we could change the hard-coded top level exception handler
(in exception.m) to print a more appropriate message if the exception is of
this type.

Cheers,
Mark.

--------------------------------------------------------------------------
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