Defensive Programming

Steve Davis steven.charles.davis@REDACTED
Mon May 17 00:42:07 CEST 2010


Hi Henning,

This isn't intended to be any kind of definite answer, and I'm
interested to see how others respond.

In my case how I deal with this question is that I'd probably ask
myself:

*Is this exceptional case _recoverable_,? e.g. in the case of the tcp
send, you could try to resend the data, so the first idiom may do it.

If it's not recoverable, I'd just let it crash out. In fact I would
likely force it to crash out using the second idiom.

If I want to get fancy with error handling, I find that try..catch at
the top level is usually enough.

A further aside to this is that a certain Mr Armstrong once introduced
me to an interesting "transactional" idiom also, namely:

try begin
...
end catch
...

Regards,

/s

On May 16, 5:04 pm, Henning Diedrich <hd2...@REDACTED> wrote:
> Hi list,
>
> I have a question about the Erlang way.
>
> I realize I keep programming defensive using exceptions/error calls to
> *label failure* more precisely.
>
> E.g. ( - PRESUMABLY WRONG - )
>
> login(Name, Password) ->
>
>        ...
>         case
>                 gen_tcp:send(Socket, term_to_binary(Str)) of
>                      ok -> ok;
>                      _ -> erlang:error(*sending_failed, {Str }*)
>        end
>        ...
>
> Instead of simply
>
> login(Name, Password) ->
>
>        ...
>         ok = gen_tcp:send(Socket, term_to_binary(Str)),
>        ...
>
> It feels wrong in Erlang and I wonder if I am missing out on something.
>
> If somebody has a pointer to read up on this, thanks in advance,
>
> Henning
>
> --
> You received this message because you are subscribed to the Google Groups "Erlang Programming" group.
> To post to this group, send email to erlang-programming@REDACTED
> To unsubscribe from this group, send email to erlang-programming+unsubscribe@REDACTED
> For more options, visit this group athttp://groups.google.com/group/erlang-programming?hl=en.


More information about the erlang-questions mailing list