catch_backtrace
catch_backtrace()
Get the backtrace of the current exception, for use within catch
blocks.
Examples
julia> try
error("Oops, something went wrong!")
catch err
bt = catch_backtrace()
println(bt)
end
ERROR: Oops, something went wrong!
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] top-level scope at REPL[1]:2
[3] eval(::Module, ::Any) at ./boot.jl:331
[4] eval_user_input(::Any, ::REPL.REPLBackend) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:139
[5] repl_backend_loop(::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:200
[6] start_repl_backend(::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:185
[7] run_repl(::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:303
[8] (::Base.var"#848#850"{Bool, Base.REPL.REPLBackend})() at ./task.jl:406
julia> try
throw(ErrorException("Custom error"))
catch err
bt = catch_backtrace()
println(bt)
end
ErrorException("Custom error")
Stacktrace:
[1] top-level scope at REPL[2]:2
[2] eval(::Module, ::Any) at ./boot.jl:331
[3] eval_user_input(::Any, ::REPL.REPLBackend) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:139
[4] repl_backend_loop(::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:200
[5] start_repl_backend(::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:185
[6] run_repl(::Any) at /buildworker/worker/package_linux64/build/usr/share/julia/stdlib/v1.6/REPL/src/REPL.jl:303
[7] (::Base.var"#848#850"{Bool, Base.REPL.REPLBackend})() at ./task.jl:406
The catch_backtrace()
function is used within catch
blocks to retrieve the backtrace of the current exception. It can be useful for logging or debugging purposes when handling exceptions.
In the first example, an error is intentionally thrown using the error
function. The catch_backtrace()
function is then called within the catch
block to retrieve the backtrace, which is then printed.
In the second example, a custom error is thrown using throw
. Again, the catch_backtrace()
function is used within the catch
block to retrieve and print the backtrace.
Note that the backtrace shows the sequence of function calls leading up to the error. This information can help in identifying the source of the error and understanding the sequence of function calls that led to the exception.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.