[erlang-questions] Exceptions and gen_server

David Mercer dmercer@REDACTED
Fri Nov 6 18:08:35 CET 2009


Is there any particular reason you want to throw the value rather than just
erroring out?  If you need to throw it to a catch, I'd probably do as you
say, but otherwise, I'd just do a match and let an error happen when it
doesn't match.  Something like:

a() ->
	{ok, Value} = gen_server:call(..., a),
	Value.

Your handle_call looks OK to me.

Cheers,

David

> -----Original Message-----
> From: erlang-questions@REDACTED [mailto:erlang-questions@REDACTED] On
> Behalf Of Anton Krasovsky
> Sent: Friday, November 06, 2009 9:57 AM
> To: erlang-questions@REDACTED
> Subject: [erlang-questions] Exceptions and gen_server
> 
> What would be the standard way of returning/throwing an exception in a
> result to a gen_server:call?
> 
> Would something like that be reasonable:
> 
> a() ->
> case gen_server:call(..., a) of
>     {ok, Value} -> Value;
>     {error, Error} -> throw({error, Error})
> end
> 
> and
> 
> handle_call(a, ...) ->
> case ... of
>     {ok, Value} -> {reply, {ok, Value} , State};
>     {error, Error} -> {reply, {error, Error} , State}
> end
> 
> Regards,
> Anton
> 
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org



More information about the erlang-questions mailing list