println
not printing stack trace, this code
try
eval(Meta.parse("invalidfn()"))
catch error
println(error)
end
produces
UndefVarError(:invalidfn)
And error.msg
or fieldnames(error)
are not working.
The printStackTrace() method of Java. lang. Throwable class used to print this Throwable along with other details like class name and line number where the exception occurred means its backtrace. This method prints a stack trace for this Throwable object on the standard error output stream.
The function printStackTrace() of the Exception class can take one parameter, either a PrintStream or a PrintWriter. Thus, it is possible, using a StringWriter, to print the stack trace into a String: StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); e. printStackTrace(pw);
You can use catch_backtrace
, together with the @error
macro from the Logging
standard library:
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
As an alternative, you can directly call the (undocumented) three-argument 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With