gpt4 book ai didi

julia - 如何打印一个错误的完整堆栈跟踪?

转载 作者:行者123 更新时间:2023-12-03 16:07:42 25 4
gpt4 key购买 nike

println不打印堆栈跟踪,此代码

try
eval(Meta.parse("invalidfn()"))
catch error
println(error)
end

产生
UndefVarError(:invalidfn)

并且 error.msgfieldnames(error)不起作用。

最佳答案

您可以将 catch_backtrace @error standard library中的Logging宏一起使用:

julia> try
eval(Meta.parse("invalidfn()"))
catch e
@error "Something went wrong" exception=(e, catch_backtrace())
end
┌ Error: Something went wrong
│ exception =
│ UndefVarError: invalidfn not defined
│ Stacktrace:
│ [1] top-level scope at REPL[1]:1
│ [2] eval at ./boot.jl:330 [inlined]
│ [3] eval(::Expr) at ./client.jl:425
│ [4] top-level scope at REPL[1]:2
│ [5] eval(::Module, ::Any) at ./boot.jl:330
│ [6] eval_user_input(::Any, ::REPL.REPLBackend) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:86
│ [7] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:118 [inlined]
│ [8] (::REPL.var"#26#27"{REPL.REPLBackend})() at ./task.jl:333
└ @ Main REPL[1]:4

或者,您可以直接调用(未记录)三参数 showerror :
julia> try
eval(Meta.parse("invalidfn()"))
catch e
showerror(stdout, e, catch_backtrace())
end
UndefVarError: invalidfn not defined
Stacktrace:
[1] top-level scope at REPL[1]:1
[2] eval at ./boot.jl:330 [inlined]
[3] eval(::Expr) at ./client.jl:425
[4] top-level scope at REPL[1]:2
[5] eval(::Module, ::Any) at ./boot.jl:330
[6] eval_user_input(::Any, ::REPL.REPLBackend) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:86
[7] macro expansion at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.3/REPL/src/REPL.jl:118 [inlined]
[8] (::REPL.var"#26#27"{REPL.REPLBackend})() at ./task.jl:333

关于julia - 如何打印一个错误的完整堆栈跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59690882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com