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

Goncalo Jorge Coelho e Silva l10454 at alunos.uevora.pt
Mon Jun 2 11:19:56 AEST 2003


Hi,

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

Here's the code I used to try it:

:- pred main(io__state, io__state).
:- mode main(di, uo) is det.

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

:- implementation.

:- import_module string.
:- import_module exception.
:- import_module require.

main(IO1, IO):-
        try_io(tryexcept(IO2, IO3), IO1, IO, Result),
        %try_io( tryexcept(IO1, IO), Result),
        %try_io( tryexcept("string", IO1, IO), Result),
        (
        Result = succeeded(IO) %,
                %io__write_string("22", IO1, IO)
        ;
        Result = exception(_),
                io__write_string(" string__det_to_int Failed! ", IO1, IO)
        ).

tryexcept(IO_0, IO):-
        string__det_to_int("D") = Int3.
        %string__det_to_int("22") = Int3,
        %io__write_string("Bla", IO_0, IO).



 I got a little bit confused with where and if to
use IO args...
 What am I to change to get rid of this? :

inter.m:022:   in argument 1 of call to predicate `try_io/4':
inter.m:022:   type error: argument has type `(pred)',
inter.m:022:   expected type was `pred(T, (io:state), (io:state))'.


and about this?
I saw the use of a Result variable in an example for 'exception IO',
but I'm affraid I got it wrong...

inter.m:022:   type error: variable `IO1' has type `(io:state)',
inter.m:022:   expected type was `(exception:exception_result(T))'.


So, I have to declare var Result as 'exception_result', is that it?

nter.m:026:   type error in unification of variable `Result'
inter.m:026:   and functor `succeeded/1'.
inter.m:026:   variable `Result' has type `(io:state)',
inter.m:026:   functor `succeeded/1' has type
inter.m:026:   `succeeded(T) :: (exception:exception_result(T))'.


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


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


Sorry about my posting avalanche phase ;)


Thanks a lot,
Goncalo

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