Exception handling idioms

Tim Bates <>
Fri Aug 26 13:44:37 CEST 2005


Ulf Wiger wrote:
> My preference is to do this:
> 
> handle_call({Atom, Int, List}, _, State)
>          when is_atom(Atom), is_integer(Int), is_list(List) ->
>      ...
>      {reply, Result, State};
>  handle_call(_, _, State) ->
>      {reply, badarg, State}.
> 
> 
> and then a custom call/1 function:
> 
> call(Request) ->
>    case gen_server:call(?SERVER, Request) of
>       badarg ->
>          erlang:error(badarg);
>       Reply ->
>          Reply
>    end.

That is nice, but what if the error is generated deep in a function 
called by handle_call? And if I want to return an error code to the 
calling process without crashing it or the server? Then I have to do 
lots of explicit {error, ...} kind of stuff which the exception handling 
mechanisms are supposed to help me avoid, or I have to wrap the contents 
of every clause of handle_call in a try. Perhaps I could extend 
gen_server to catch non-local returns, ie throw(), and re-raise them in 
the calling process. But that still doesn't work for gen_server:cast(). 
Any ideas?

Tim.

-- 
Tim Bates




More information about the erlang-questions mailing list