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)

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
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-----
[mailto:]On Behalf Of Vladimir Sekissov
Sent: den 13 januari 2002 19:00
Subject: Avoid case nesting

Good day,

To avoid many nested cases I use following simple constract:

case catch
        {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} ->

Is there better and more elegant approaches?

Best Regards,

Vladimir Sekissov

More information about the erlang-questions mailing list