Print the current stacktrace to stdout without throwing an exception

If I throw an exception, it prints the full stacktrace at the current point.

Is there a way to print the current stacktrace to stdout without throwing an exception?

Here’s a little more detail to illustrate what I mean.

Suppose I run the following code in the REPL:

julia> function f()
       g()
       end
f (generic function with 1 method)

julia> function g()
       h()
       end
g (generic function with 1 method)

julia> function h()
       throw(ErrorException("hello world"))
       end
h (generic function with 1 method)

julia> f()
ERROR: hello world
Stacktrace:
 [1] h()
   @ Main ./REPL[2]:2
 [2] g()
   @ Main ./REPL[1]:2
 [3] f()
   @ Main ./REPL[1]:2
 [4] top-level scope
   @ REPL[3]:1

When the exception is thrown, it prints the full stacktrace to stdout.

Is there any way to get the same output (the full stacktrace, printed to stdout) without throwing an exception?

There is Base.show_backtrace(stdout, backtrace()). Is this what you want?

1 Like

you could put h() into a task like that

julia> t = @async h();

julia> t
Task (failed) @0x0000000113eb7a90
hello world
h() at ./REPL[21]:2
(::var"#7#8")() at ./task.jl:356

julia> t.exception
ErrorException("hello world")

if you define g() = @async h(), you get the task back like above without a exception thrown in the current scope.

julia> f()
Task (failed) @0x0000000114145b10
hello world
h() at ./REPL[21]:2
(::var"#11#12")() at ./task.jl:356
1 Like

Yep, this is exactly what I was looking for!

julia> function f()
       g()
       end
f (generic function with 1 method)

julia> function g()
       h()
       end
g (generic function with 1 method)

julia> function h()
       Base.show_backtrace(stdout, backtrace())
       end
h (generic function with 1 method)

julia> f()

Stacktrace:
  [1] h()
    @ Main ./REPL[3]:2
  [2] g
    @ ./REPL[2]:2 [inlined]
  [3] f()
    @ Main ./REPL[1]:2
  [4] top-level scope
    @ REPL[4]:1
  [5] eval
    @ ./boot.jl:369 [inlined]
  [6] eval_user_input(ast::Any, backend::REPL.REPLBackend)
    @ REPL ~/dev/repos/JuliaLang/julia/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:139
  [7] repl_backend_loop(backend::REPL.REPLBackend)
    @ REPL ~/dev/repos/JuliaLang/julia/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:200
  [8] start_repl_backend(backend::REPL.REPLBackend, consumer::Any)
    @ REPL ~/dev/repos/JuliaLang/julia/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:185
  [9] run_repl(repl::REPL.AbstractREPL, consumer::Any; backend_on_current_task::Bool)
    @ REPL ~/dev/repos/JuliaLang/julia/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:317
 [10] run_repl(repl::REPL.AbstractREPL, consumer::Any)
    @ REPL ~/dev/repos/JuliaLang/julia/usr/share/julia/stdlib/v1.7/REPL/src/REPL.jl:305
 [11] (::Base.var"#891#893"{Bool, Bool, Bool})(REPL::Module)
    @ Base ./client.jl:394
 [12] #invokelatest#2
    @ ./essentials.jl:710 [inlined]
 [13] invokelatest
    @ ./essentials.jl:708 [inlined]
 [14] run_main_repl(interactive::Bool, quiet::Bool, banner::Bool, history_file::Bool, color_set::Bool)
    @ Base ./client.jl:379
 [15] exec_options(opts::Base.JLOptions)
    @ Base ./client.jl:309
 [16] _start()
    @ Base ./client.jl:492
1 Like