[erlang-questions] Unexpected try/catch behaviour

Raimo Niskanen <>
Wed Feb 24 09:56:09 CET 2010


On Wed, Feb 24, 2010 at 12:11:44PM +1300, Richard O'Keefe wrote:
> 
> On Feb 24, 2010, at 12:36 AM, Attila Rajmund Nohl wrote:
> 
> >2010/2/23, Ulf Wiger <>:
> >[...]
> >>From the Erlang Reference Manual:
> >>
> >>
> >>An exception occurring during the evaluation of Body is not caught."
> >
> >This is um, surprising, even by the standards of Erlang... I mean not
> >the documentation, but the actual language construct.
> 
> I had forgotten about try...of...catch...end.
> When is it appropriate to use try E of H catch ... end
> instead of try case E of H end catch ... end?
> 
> I think the _intent_ is clear, that in try E ... end
> it is *only* E that is protected.
> 
> It does make sense, but it would be really nice to see
> a couple of examples where try .. of .. catch .. end was
> the right thing to do.

I think this kind of construct is harder to do without the of..catch part:

Handle = resource:open("foo.txt"),
try resource:read(Handle) of
    {ok, Data} ->
	foo(Data);
    Error ->
	Error
catch
    error:Reason ->
	{error,Reason}
after
    resource:close(Handle)
end

> 
> 
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list