I have a long running application with custom loggers set up, and when an error is thrown, I want to log that nicely with @error
. This is done in a catch
block, and works well. Since @error
does not throw an error itself, I added rethrow()
, because I want a non-zero exit code. What I don’t like however is that I then get two stacktraces in the terminal.
function f()
try
error("oh no")
catch e
@error "Something went wrong" exception = (e, catch_backtrace())
rethrow()
end
end
f()
┌ Error: Something went wrong
│ exception =
│ oh no
│ Stacktrace:
│ [1] error(s::String)
│ @ Base .\error.jl:33
│ [2] f()
│ @ Main C:\tmp\err\err.jl:3
│ [3] top-level scope
│ @ C:\tmp\err\err.jl:10
│ [4] include(mod::Module, _path::String)
│ @ Base .\Base.jl:418
│ [5] exec_options(opts::Base.JLOptions)
│ @ Base .\client.jl:292
│ [6] _start()
│ @ Base .\client.jl:495
└ @ Main C:\tmp\err\err.jl:5
ERROR: LoadError: oh no
Stacktrace:
[1] error(s::String)
@ Base .\error.jl:33
[2] f()
@ Main C:\tmp\err\err.jl:3
[3] top-level scope
@ C:\tmp\err\err.jl:10
in expression starting at C:\tmp\err\err.jl:10
Is there a way to avoid swallowing the exception but not have it printed a second time? Of course exit(1)
instead of rethrow()
would work but I don’t want it to quit julia.