[mercury-users] using 'try' for exception handling

Fergus Henderson fjh at cs.mu.OZ.AU
Mon Jun 2 11:39:55 AEST 2003


On 02-Jun-2003, Goncalo Jorge Coelho e Silva <l10454 at alunos.uevora.pt> wrote:
> 
> I was searching the archive on this subject, found
> something, but could use that to solve my problem.
> 
> I'm trying to make/use a predicate that would 
> catch (using 'try') the exception thrown by an
> incorrect use of 'string__det_to_int' (for example).

In that specific case, you're probably better off
calling string__to_int rather than string__det_to_int.

> Here's the code I used to try it:
...
> :- pred tryexcept(io__state, io__state).
> :- mode tryexcept(di, uo) is det.
> %:- pred tryexcept(string ,io__state, io__state).
> %:- mode tryexcept(in, di, uo) is det.

The commented out version is the one that you need.

> main(IO1, IO):-
>         try_io(tryexcept(IO2, IO3), IO1, IO, Result),
>         %try_io( tryexcept(IO1, IO), Result),
>         %try_io( tryexcept("string", IO1, IO), Result),

The syntax that you are looking for here is

	try_io(try_except, Result, IO1, IO)

> Also,
> 
> I noticed on bit of Fergus' code, the following lines:
> 
> >		else
> >			require.error("make_term_with_arglist failed")
> 
>  I tried this out but got a message saying I was using 
> the '.' on a bizarre place, or something like that...

Oh, sorry, that syntax has been added recently, but I forgot that
it is not yet present in the current official release (0.11.0).
Use "require__error" instead.  Same goes for all other uses of "." as
a module qualifier -- use "__" instead.

> When should I use try/throw VS require.error?

Generally require.error should be used for situations that "cannot happen",
i.e. those for which if the situation did occur, it would indicate a bug
in (some other part of) the program.  "throw" can be used for any other
kinds of errors that you want to deal with using exception handling,
e.g. I/O errors, network timeouts, etc.

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list