Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write an exception stack trace in erlang after catching it?

Suppose I have something like this :

try code_that_fails()
catch _:_ -> .....

How do I print the stacktrace in the catch block? That block catches all exceptions, but I don't know how to print the stack...

Can you help me?

like image 340
Francesco Avatar asked Aug 26 '09 15:08

Francesco


People also ask

How do I print a stack trace for exceptions?

Using printStackTrace() method − It print the name of the exception, description and complete stack trace including the line where exception occurred. Using toString() method − It prints the name and description of the exception. Using getMessage() method − Mostly used. It prints the description of the exception.

What is stack trace in exception?

A trace of the method calls is called a stack trace. The stack trace listing provides a way to follow the call stack to the line number in the method where the exception occurs. The StackTrace property returns the frames of the call stack that originate at the location where the exception was thrown.

What does end of stack trace from previous location where exception was thrown mean?

When the exception is restored, the following string is inserted in the stack trace to indicate the restore point: "End of stack trace from the previous location where the exception was thrown". This is similar to the way inner exceptions or marshaled exceptions are indicated in stack traces.


1 Answers

From Erlang 21.0 onwards, there's a new official way to get the stack trace. An optional pattern match in the try expression on the third parameter in the exception, which will contain the stack trace:

try
   code_that_fails()
catch
   _:_:Stacktrace ->
      erlang:display(Stacktrace)
end

Older versions (OTP 20 and below)

For versions of Erlang/OTP 20 and below, you need to use get_stacktrace/0, which allows you to get the stacktrace of the last exception in the calling process:

try
   code_that_fails()
catch
   _:_ ->
      erlang:display(erlang:get_stacktrace())
end
like image 162
Christian Avatar answered Sep 22 '22 15:09

Christian