Structs

Luke Gorrie luke@REDACTED
Thu Jan 16 13:04:41 CET 2003


Miguel Barreiro Paz <enano@REDACTED> wrote:
> 	I suppose exception throwing mechanisms were invented among other
> reasons to simplify the horrible code that results from defensive
> programming; ie., after a few burnouts C programmers turn every function
> call and assignment line into ten lines or so (check whether results are
> valid, then do something sensible if they aren't, continue otherwise).
> Now, languages like Java do have exception throwing mechanisms, but many
> programmers insist on C-style manual result checking.

I still have problems in Erlang in the cases where I don't want to
crash, i.e. the non-assertion type of error detection. Here's a
function I wrote recently:

  connect(Host, User, Password) ->
      case otp_ftp:open(Host) of
          {ok, Pid} ->
              link(Pid),
              case otp_ftp:user(Pid, User, Password) of
                  ok ->
                      case otp_ftp:pwd(Pid) of
                          {ok, Home} ->
                              ?event(ftp, "~p: Home is ~p", [self(), Home]),
                              case otp_ftp:type(Pid, binary) of
                                  ok ->
                                      %% It is at about this level of nesting
                                      %% that better exception handling in
                                      %% erlang becomes appetizing..
                                      {ok, Pid, Home};
                                  {error, Rsn} ->
                                      {error, otp_ftp:formaterror(Rsn)}
                              end;
                          {error, Rsn} ->
                              {error, otp_ftp:formaterror(Rsn)}
                      end;
                  {error, Rsn} ->
                      {error, otp_ftp:formaterror(Rsn)}
              end;
          {error, Rsn} ->
              {error, otp_ftp:formaterror(Rsn)}
      end.

Cheers,
Luke




More information about the erlang-questions mailing list