[mercury-users] Exception handling in Mercury.

Bartlomiej Szymczak rhywek at gmail.com
Fri Jun 29 19:30:00 AEST 2007


On 6/29/07, Ralph Becket <rafe at csse.unimelb.edu.au> wrote:
> Bartlomiej Szymczak, Thursday, 28 June 2007:
> > Hi, all.
> >
> > 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.
>
> You should have seen something printed out telling you what the uncaught
> exception was.
>
> However, from what you say catching misses via exceptions sounds like
> the wrong approach: exceptions should be used for exceptional
> circumstances, not expected circumstances.  You should consider instead
> using hash_table.search(HashTable, Key, Value), which will simply fail
> if there is no mapping for Key in HashTable.
>
> -- Ralph
> --------------------------------------------------------------------------
> 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
> --------------------------------------------------------------------------
>

I think it's a good approach, because there must be an entry in the
hash table for each key I'm looking for. If there is no entry, I don't
even want the program to continue, as I have messed something up when
entering the huge lexicon by hand into the hash table.

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