# Print the current stacktrace to stdout without throwing an exception

**URL:** <https://discourse.julialang.org/t/print-the-current-stacktrace-to-stdout-without-throwing-an-exception/53107>\
**Category:** New to Julia\
**Created:** [January 10, 2021, 7:36am UTC](https://discourse.julialang.org/t/print-the-current-stacktrace-to-stdout-without-throwing-an-exception/53107 "2021-01-10T07:36:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [January 10, 2021, 7:36am UTC](https://discourse.julialang.org/t/print-the-current-stacktrace-to-stdout-without-throwing-an-exception/53107/1 "2021-01-10T07:36:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [January 10, 2021, 7:39am UTC](https://discourse.julialang.org/t/print-the-current-stacktrace-to-stdout-without-throwing-an-exception/53107/2 "2021-01-10T07:39:01Z")

</div>

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

Suppose I run the following code in the REPL:

```julia
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?

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [January 10, 2021, 8:17am UTC](https://discourse.julialang.org/t/print-the-current-stacktrace-to-stdout-without-throwing-an-exception/53107/3 "2021-01-10T08:17:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)\
**Post date:** [January 10, 2021, 8:25am UTC](https://discourse.julialang.org/t/print-the-current-stacktrace-to-stdout-without-throwing-an-exception/53107/4 "2021-01-10T08:25:36Z")

</div>

you could put `h()` into a task like that

```julia
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
julia> f()
Task (failed) @0x0000000114145b10
hello world
h() at ./REPL[21]:2
(::var"#11#12")() at ./task.jl:356

```

---

<div class="post-metadata">

**Author:** ![dilumaluthge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dilumaluthge/32/29283_2.png) [@dilumaluthge](https://discourse.julialang.org/u/dilumaluthge)\
**Post date:** [January 10, 2021, 8:49am UTC](https://discourse.julialang.org/t/print-the-current-stacktrace-to-stdout-without-throwing-an-exception/53107/5 "2021-01-10T08:49:51Z")

</div>

> [@thofma](#):
>
> There is `Base.show_backtrace(stdout, backtrace())` . Is this what you want?

Yep, this is exactly what I was looking for!

```julia
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

```
