<br><br><div class="gmail_quote">On Mon, Mar 23, 2009 at 7:58 PM, Adam Lindberg <span dir="ltr"><<a href="mailto:adam@erlang-consulting.com">adam@erlang-consulting.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im"><br>
----- "Hynek Vychodil" <<a href="mailto:vychodil.hynek@gmail.com">vychodil.hynek@gmail.com</a>> wrote:<br>
</div><div class="im">> If I have function like<br>
><br>
> is_new_email(Email) -><br>
>  case mnesia:dirty_read(email, Email) of<br>
>    []    -> ok;<br>
>    _Else -> email_in_use<br>
>  end.<br>
><br>
> I can choose if use it as<br>
><br>
> case is_new_email(Email) of<br>
>   ok -> do_something();<br>
>   email_in_use -> do_something_other()<br>
> end<br>
<br>
</div>This could be rewritten with a try and a throw (as I previously showed), so the developer is still able to choose. It is about the same amount of code as a case statement. The try statement also have the benefit of being able to fit more lines of code in a single statement than catch can.<br>

<div class="im"><br>
><br>
> or as<br>
><br>
> ok = is_new_email(Email);<br>
<br>
</div>This will still throw a badmatch when the email address is in use. I don't see how that is so much better than a throw. Both will go uncatched if you don't do anything.<br>
<div class="im"><br>
> When You trow exception, I can't choose.<br>
> Don't make assumptions about what the caller will do with the results<br>
> of a<br>
> function (<a href="http://www.erlang.se/doc/programming_rules.shtml#HDR3" target="_blank">http://www.erlang.se/doc/programming_rules.shtml#HDR3</a>)<br>
<br>
</div>Like I said, there is no big difference. I just like throw since it allows for bigger flexibility when combining several statements.<br>
<br>
In the end, I guess it is a matter of taste and style.<br>
<br>
Cheers,<br>
<font color="#888888">Adam<br>
</font></blockquote></div><br>1/ Exception costs more than checking return value. In most cases it will not be issues but you can't assume that it will not be. When it happen, your code is less flexible.<br>2/ Flexibility is throw out when I want code like: is_new_email(Email1), is_new_email(Email2).<br>
Now I can't distinguish which one Email is wrong without catching throw in each statement. With my code it is easy:<br>{ok, first_one} = {is_new_email(Email1), first_one},<br>{ok, second_one} = {is_new_email(Email2), second_one}.<br>
Again, your code is less flexible.<br><br>Throwing exception when it is not necessary is less flexible. I can easily turn return value to exception but opposite way is harder. Explicit throwing exception *is* slower and less flexible!<br clear="all">
<br>-- <br>--Hynek (Pichi) Vychodil<br><br>Analyze your data in minutes. Share your insights instantly. Thrill your boss.  Be a data hero!<br>Try Good Data now for free: <a href="http://www.gooddata.com">www.gooddata.com</a><br>