捕获后如何在erlang中编写异常堆栈跟踪? [英] How can I write an exception stack trace in erlang after catching it?
问题描述
假设我有这样的事情:
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
这篇关于捕获后如何在erlang中编写异常堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!