[mercury-users] Exception handling in Mercury.

Julien Fischer juliensf at csse.unimelb.edu.au
Thu Jun 28 21:55:53 AEST 2007


On Thu, 28 Jun 2007, Bartlomiej Szymczak wrote:

> My mercury code uses the hash_table module. I've read the library
> reference for it, and it says:
>
>        % Lookup the value associated with the given key. An
> exception is raised
>        % if there is no entry for the key.
>        %
>    :- func lookup(hash_table(K, V), K) = V.
>    :- mode lookup(hash_table_ui, in) = out is det.
>
> Guess what - indeed the exception was raised when I was testing my
> program. The program simply terminated. I've got excited, as I didn't
> know there were exceptions in Mercury.
>
> I was trying to find a way of handling this exception, but I couldn't
> find anything like "handle" in SML or "try"+"catch" in C++/Java.

The Mercury exception handling stuff is in the standard library
module `exception'.  The predicate throw/1 is used to throw
exceptions and the various forms of the try predicate are used to
catch them.

> Is there any way of handling this exception?

Use the predicate exception.try/2.  (I think that exception should
have the type software_error/1, but I'm not sure - the documentation
for that predicate should really be changed to say what type is.)

> But even more useful thing for me would be raising exceptions from my
> own code. Is there such a possibility?

Of course, use the predicate exception.throw/1 I mentioned above.

Cheers,
Julien.
--------------------------------------------------------------------------
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