Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print full stack trace of an Error?

Tags:

julia

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.

like image 670
Alex Craft Avatar asked Jan 11 '20 01:01

Alex Craft


People also ask

How do I print a stack trace error?

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.

How do I print stack trace logs?

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);


1 Answers

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
like image 85
giordano Avatar answered Oct 22 '22 02:10

giordano