safe catch

Richard Carlsson <>
Mon Mar 25 13:41:49 CET 2002


On Mon, 25 Mar 2002, Ulf Wiger wrote:

> I decided to put in the following construct:
> 
>   case catch {ok, f()} of
>      {'EXIT', Reason} ->
>         handle_EXIT(Reason);
>      {ok, Result} ->
>         Result
>   end.
> 
> If all one cares about is handling the case where the caught
> function really exits, I don't really see a reason *not* to
> consistently use the above pattern.
> 
> Does anyone have a suggestion for a fool proof and "free" way to
> do this?

Well, TANSTAAFL, I'm afraid. I always use the {ok, Expr} idiom, and
although it is a bother, it works well and does not really classify as
"expensive".

> Of course (sigh), the caught function could still mess things up by
> calling throw({ok, {'EXIT', Reason}}), but there should perhaps be a
> limit to the level of stupidity one needs expect from the average
> programmer. Here's a paranoid (but more expensive) version:
> 
>   Ref = make_ref(),
>   case catch {Ref, f()} of

Yes, that's the way to do it if you *really* want to make sure nobody
can fool your code. But (although I don't think there are any such
limits as you mention) I don't think there is any call for it in normal
code. And it's certainly more expensive, if you put it it a tight(ish)
loop.

More exact error handling will be possible in future versions of Erlang,
when we get the "try ... catch ..." stuff implemented. Real Soon Now.

	/Richard


Richard Carlsson ()   (This space intentionally left blank.)
E-mail: 	WWW: http://www.csd.uu.se/~richardc/

"Having users is like optimization: the wise course is to delay it."
   -- Paul Graham




More information about the erlang-questions mailing list