In your example, you don't need the try ; you can just do
result = (catch code_that_fails()).
If an exception is raised, catch returns a tuple that contains the error code and stack trace. Note that this is generally considered bad practice as it can mask exceptions. The stacktrace approach described in another answer is almost certainly what you want. try is an extension of the original catch functionality; if you use it, you need to specify clauses for each exception type you would like to catch, and handle them appropriately. See sections 6.18/6.19 of the Erlang reference manual for details and clear examples.
3条答案
按热度按时间hwamh0ep1#
从Erlang 21.0开始,有了一种新的官方方法来获取堆栈跟踪。在异常的第三个参数上的try表达式中有一个可选的模式匹配,它将包含堆栈跟踪:
旧版本(OTP 20及以下版本)
对于Erlang/OTP 20及以下版本,您需要使用get_stacktrace/0,它允许您获取调用进程中最后一个异常的堆栈跟踪:
nkcskrwz2#
您问题的答案是:
当前函数位于列表的开头。请阅读
man 3erl erlang
或erlang:get_stacktrace/0中的更多内容vd8tlhqk3#
In your example, you don't need the
try
; you can just doIf an exception is raised,
catch
returns a tuple that contains the error code and stack trace.Note that this is generally considered bad practice as it can mask exceptions. The stacktrace approach described in another answer is almost certainly what you want.
try
is an extension of the originalcatch
functionality; if you use it, you need to specify clauses for each exception type you would like to catch, and handle them appropriately. See sections 6.18/6.19 of the Erlang reference manual for details and clear examples.