[erlang-questions] try .. of .. catch question

Dave Challis <>
Wed Feb 9 13:19:43 CET 2011


Ah, thanks for the answers, than clears up a lot of things!

On 09/02/11 12:15, Raimo Niskanen wrote:
> One more subtle difference: in the first block if foo() returns
> something other than true or false, the case clause generated
> will be caught by the catch clause, but in the second block
> a try clause will be generated and not caught by the catch
> clause since it out of scope of the try..catch.
>
> On Wed, Feb 09, 2011 at 12:31:25PM +0100, Jeroen Koops wrote:
>> Hi Dave,
>>
>> The two blocks are different. In the first block, if an exception occurs
>> anywhere between the 'case' and the 'end', it will be caught by the
>> catch-clause. But in the second block, only exceptions occurring when
>> evaluating foo() will be caught; exceptions occuring while evaluating 'a' or
>> 'b' won't.
>>
>> Regards,
>>
>> Jeroen
>>
>>
>>
>> On Wed, Feb 9, 2011 at 12:22 PM, Dave Challis<>  wrote:
>>
>>> Hi,
>>> I'm just learning about exceptions in Erlang, and just curious as to the
>>> use of try..of..catch - is it just a shortcut to combining a case with a
>>> try/catch?
>>>
>>> For example, are the two blocks of code below identical? Or is there some
>>> subtle difference I've missed?
>>>
>>> try
>>>     case foo() of
>>>         true  ->  a;
>>>         false ->  b
>>>     end
>>> catch
>>>     Error:Reason ->  {Error, Reason}
>>> end
>>>
>>>
>>> try
>>>     foo()
>>> of
>>>     true  ->  a;
>>>     false ->  b
>>> catch
>>>     Error:Reason ->  {Error, Reason}
>>> end
>>>
>>> Thanks,
>>> --
>>> Dave Challis
>>> 
>>>
>>> ________________________________________________________________
>>> erlang-questions (at) erlang.org mailing list.
>>> See http://www.erlang.org/faq.html
>>> To unsubscribe; mailto:
>>>
>>>
>


-- 
Dave Challis



More information about the erlang-questions mailing list