捕获后如何在erlang中编写异常堆栈跟踪?堆栈、异常、如何在、erlang

由网友(清泪入喉)分享简介:假设我有这样的事情:try code_that_fails()catch _:_ -> .....如何在 catch 块中打印堆栈跟踪?该块捕获所有异常,但我不知道如何打印堆栈...How do I print the stacktrace in the catch block? That block catch...

假设我有这样的事情:

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

如何在 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...

你能帮帮我吗?

推荐答案

从 Erlang 21.0 开始,有一个新的官方方法来获取堆栈跟踪.异常中第三个参数的 try 表达式 中的可选模式匹配,其中将包含堆栈跟踪:

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

旧版本(OTP 20 及以下)

对于 Erlang/OTP 20 及以下的版本,您需要使用 get_stacktrace/0,可以获取调用进程中最后一个异常的stacktrace:

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
阅读全文

相关推荐

最新文章