using catch - getting error

Wes James <>
Sat Jun 26 00:16:27 CEST 2010


I have this function:

lk(Key, L) ->
    {_, Error} = (catch {value, {_,Vale}} = lists:keysearch(Key, 1, L)),
    case length(Error) of
    	0 ->
    		Val = Vale;
    	_ ->
    		Val = 0
    end,
    Val.

A list (L) should have all the keys necessary when lk is called, but
if the key does not exist, the keysearch will throw an exception.  I'm
trying to catch that, but I get the error Vale is unsafe. What is the
proper way to do this?

If the key exists, then put it in Val and return Val, if the key does
not exist, catch the error and set Val to 0.

How should this be done?

thx,

-wes


More information about the erlang-questions mailing list