[erlang-questions] erlang:raise/3 considered harmful?
Mon May 16 12:19:12 CEST 2016
On Sat, May 14, 2016 at 1:48 PM, Per Hedeland <> wrote:
> I happened to want something like erlang:raise/3 for a library function
> - I didn't think it existed:-), but found that it does, although it
> comes with a semi-dire warning. The loop/3 function below does exactly
> what I want, specifically: if the fun provided by the caller of
> interate/3 crashes, the caller gets the same exception as if there had
> been no try ... catch, can handle it with hir own try/catch or not, but
> in any case receives full information about *what* went wrong in hir fun
> - all while the "protocol" spoken over the socket remains in sync.
> So, to take heed of the warning - is there a way to write an equivalent
> function *without* using raise/3 - or should it just be considered a
> case of "you really know what you are doing"? I believe I am of
> course:-), but I also think that the code below is a perfectly
> reasonable implementation of a side-effecty library function, and has no
> more to do with debugging or knowing what you're doing than any other
I agree. I will remove the warning in OTP 19.
erlang:raise/3 is particularly useful when you
don't want to handle the error, but let you log
important information about the context.
For example, most compiler passes in the
BEAM compiler use code similar to:
%% Optimize the code.
catch Class:Error ->
Stack = erlang:get_stacktrace(),
io:format("Function: ~w/~w\n", [Name,Arity]),
erlang:raise(Class, Error, Stack)
Björn Gustavsson, Erlang/OTP, Ericsson AB
More information about the erlang-questions