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

前端 未结 3 1296
借酒劲吻你
借酒劲吻你 2021-02-05 09:52

Suppose I have something like this :

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

How do I print the stacktrace in the catch block? That block c

3条回答
  •  没有蜡笔的小新
    2021-02-05 10:54

    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
    

提交回复
热议问题