Stack Traces

When an exception is caught, we can find the latest stack trace by calling erlang:get_stacktrace(). Here’s an example:

try_test.erl
 
demo3() ->
 
try​ generate_exception(5)
 
catch
 
error:X ->
 
{X, erlang:get_stacktrace()}
 
end​.
 
1>​ try_test:demo3().
 
{a,[{try_test,generate_exception,1,[{file,"try_test.erl"},{line,9}]},
 
{try_test,demo3,0,[{file,"try_test.erl"},{line,33}]},
 
{erl_eval,do_apply,6,[{file,"erl_eval.erl"},{line,576}]},
 
{shell,exprs,7,[{file,"shell.erl"},{line,668}]},
 
{shell,eval_exprs,7,[{file,"shell.erl"},{line,623}]},
 
{shell,eval_loop,3,[{file,"shell.erl"},{line,608}]}]}

The previous trace shows what happened when we tried to evaluate try_test:demo3(). It shows that our program crashed in the function ...

Get Programming Erlang, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.