no more tagged returns
Raimo Niskanen
raimo@REDACTED
Tue Feb 22 08:43:31 CET 2005
erlang:error/1,2 is new and useful for exactly what you described,
but erlang:fault/1,2 has been around quite a while, and it is exactly
the same. erlang:error/1,2 was introduced along with
try..of..catch..after..end just to get a name on erlang:fault/1,2
that harmonized with the class of the exception it raises.
This will match for any term (in R10B):
Term = try erlang:Class(Term)
catch Class:Term -> Term
end,
for Class = error | throw | exit
vances@REDACTED (Vance Shipley) writes:
> Today I discovered erlang:error/2 (apparently added in R10) which
> completes what is needed to write code which behaves in the way it
> really should when called with the wrong arguments. Nice.
>
> I like to write all API functions with as many clause guards as
> possible(*) to make most effect use of the error reports. Now I
> can write the clause guards in the server where the procedures are
> implemented.
>
> -Vance
>
> (*) The "fail fast" principle.
>
>
> -module(new_api).
> -behaviour(gen_server).
> -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
> terminate/2, code_change/3]).
> -export([foo_to_list/2]).
>
>
> foo_to_list(S, Arg) ->
> case gen_server:call(S, Arg) of
> {error, Reason} ->
> erlang:error(Reason, [S, Arg]);
> Result ->
> Result
> end.
>
> init(_) ->
> {ok, []}.
>
> handle_cast(_Event, State) ->
> {noreply, State}.
>
> handle_call(foo, _From, State) ->
> {reply, atom_to_list(foo), State};
> handle_call(Atom, _From, State) when is_atom(Atom) ->
> {reply, {error, function_clause}, State};
> handle_call(_Event, _From, State) ->
> {reply, {error, bardarg}, State}.
>
> handle_info(_Info, State) ->
> {noreply, State}.
>
> terminate(_Reason, _State) ->
> ok.
>
> code_change(_OldVsn, State, _Extra) ->
> {ok, State}.
>
--
/ Raimo Niskanen, Erlang/OTP, Ericsson AB
More information about the erlang-questions
mailing list