On Dec 6, 2010, at 10:22 AM, Paul Davis wrote:
> On Mon, Dec 6, 2010 at 10:19 AM, Adam Kocoloski <[email protected]> wrote:
>> On Dec 5, 2010, at 5:34 AM, Paul Davis wrote:
>>
>>> On Sun, Dec 5, 2010 at 3:19 AM, Jason Smith <[email protected]> wrote:
>>>> On Sun, Dec 5, 2010 at 6:06 AM, <[email protected]> wrote:
>>>>> + case catch Fun(Doc, Req) of
>>>>> + true -> true;
>>>>> + false -> false;
>>>>> + {'EXIT', Error} -> ?LOG_ERROR("~p", [Error])
>>>>> + end
>>>>
>>>> The O'Reilly book _Erlang Programming_ suggests that catch expressions
>>>> (the older form before try...catch came out) are not as elegant as
>>>> try...catch.
>>>>
>>>> http://books.google.com/books?id=Qr_WuvfTSpEC&pg=PA74&lpg=PA74&source=bl&ots=aK-DfyxREb&sig=LA1Fi-lSKEPJNvMFdp0kXzOxDg8&hl=en&ei=W0T7TJLpMY_QrQffr4HBCA&sa=X&oi=book_result&ct=result&resnum=1&ved=0CBIQ6AEwAA#v=onepage&q&f=false
>>>>
>>>> Also Joe Armstrong's book states that you lose a lot of precision
>>>> analyzing the cause of an error.
>>>>
>>>> FYI.
>>>>
>>>> --
>>>> Jason Smith
>>>> CouchOne Hosting
>>>>
>>>
>>> I'm not sure about the book's definition of elegant, but:
>>>
>>> case (catch Fun()) of
>>> Val1 -> foo;
>>> Val2 -> bar;
>>> {Error, Reason} -> other
>>> end
>>>
>>> Seems more readable than:
>>>
>>> try
>>> case Fun() of
>>> Val1 -> foo;
>>> Val2 -> bar
>>> end
>>> catch
>>> throw:{Error, Reason} -> other
>>> end
>>>
>>> Its true that you end up collapsing the Error and Response domains
>>> into a single namespace, but in general, most things don't look like
>>> errors.
>>
>> It can be made simpler:
>>
>> try Fun() of
>> Val1 -> foo;
>> Val2 -> bar
>> catch
>> {Error, Reason} -> other
>> end
>>
>>
>
> That's not bad. I've never seen "try F() of" before. Is it new or am I blind?
Dunno. It's been around as long as I remember. One thing to be careful of -
the statement you wrote actually allows a user to catch exceptions from
functions called in the body of the try clause, e.g.
try (case Fun() of
Val1 -> G();
Val2 -> bar
end) catch
ExceptionThrownByG -> ok
end
whereas the "try F() of" syntax will not catch anything thrown by G(). This
has burned me before.
Adam