Avoid case nesting

Thomas Lindgren <>
Mon Jan 14 10:14:08 CET 2002


All I can think of is wrapping things up a bit more.

case catch begin ... end of
   {ok, Res} -> Res;
   Err -> handle_error(Err)
end

return_error({'EXIT', ...}) -> %% and so on

There is a common further difficulty, however: when you have a
state that needs to be processed if there is an error,
you also must ensure that the state is thrown with the
rest of the error. (For the example above, if you want to log the current
state
record at an error inside the begin ... end, handle_error
has to get that state record somehow.)

The obvious solution to this "state visibility problem" is
to write the log where the error occurs, which naturally
leads to nested cases. Perhaps someone has a better solution?

-- Thomas

-----Original Message-----
From: 
[mailto:]On Behalf Of Vladimir Sekissov
Sent: den 13 januari 2002 19:00
To: 
Subject: Avoid case nesting


Good day,

To avoid many nested cases I use following simple constract:

case catch
    begin
        {ok, MgrPid} = start_link(), %if wrong we'll get badmatch here
        ...
        {ok, GoodResult}
    end of
    {'EXIT', {{badmatch, {error, Error}}, _}} ->
        {error, Error};
    {'EXIT', {{badmatch, Error}, _}} ->
        {error, Error};
    {'EXIT', Error} ->
            {error, Error};
    {ok, Result} ->
        Result
end.

Is there better and more elegant approaches?

Best Regards,

Vladimir Sekissov




More information about the erlang-questions mailing list