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

前端 未结 3 1237
借酒劲吻你
借酒劲吻你 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:57

    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.

提交回复
热议问题