Behaviour of exit

Joe Armstrong joe@REDACTED
Mon Jan 28 13:23:05 CET 2002


Can anybody explain the behaviour of exit?

If I write:

fac(0) -> 1;
fac(N) -> N*fac(N-1).

g() ->
    (catch fac(abc)).

Then the value of g() is 

{'EXIT',{badarith,[{examples,fac,1},
                         {examples,g,0},
                         {erl_eval,expr,3},
                         {erl_eval,exprs,4},
                         {shell,eval_loop,2}]}}

But I had *expected* it to be something 
like:{'EXIT',{badarith,{examples,fac,1}}

The error occurs in fac/1 and is caught in g/0 I had imagined
that compiling g/0 would cause a "catch point" to be
added to the stack, and that the exception
would propagate up the stack and be caught in g/0.

It seems very strange that the exception has contextual
information about the stuff *above* g in the call sequence
(i.e. that g was called from the shell) this is because
wrapping a call in a catch (i.e. writing (catch fac(abc)))
is supposed to complete contain the error in a
"downwards" sence - so that the evaluation of
g() is not in error.

The example on pages 86 + 87 of the Erlang book
(2'nd ed) is now invalid.

14> examples:demo(3).

{caught_error,{badarg,[{erlang,tuple_to_list,[a]},
                       {examples,foo,1},
                       {examples,demo,1},
                       {erl_eval,expr,3},
                       {erl_eval,exprs,4},
                       {shell,eval_loop,2}]}}

But the book says it should be

{caught_error, badarg}

Admittedly the *exact* arguments of {'EXIT', ....} have never been
precisely defined - but I would have expected that an exception
generated within  (catch F) could never contain information
from ouside the scope of the call to F.

If we return to the fac/1 example, the error occurs when I try to do 
arithmetic on an atom i.e. I call fac(N) -> N*fac(N-1) with N = abc
this makes the recursive call fac(N-1) and tries to compute abc-1. 

At this point I would expect the exception to be
{badarith,'-',1,abc} (say) and I'd expect the *value* of (catch fac(abc)) to
be {'EXIT',{badarith,'-',1,abc}}. I do not expect to be told the *context* of 
the call (from the shell) - this is irrelevant.

It would seem desirable that
the exeption given when '-' has an incorrect
argument to be {badarith,'-',Arg1, Arg2}.

I guess I can't ask "what does the spec say?" - since this area of the system 
doesn't seem to be documented (or is it?)

/Joe





More information about the erlang-questions mailing list