Jonas
Jonas

Reputation: 1529

Why does julia throw an LoadError in addition to the error I want to throw

If I write a script that does nothing else than throwing an error like this:

throw(ErrorException("a useful message"))

I get the following error when I execute the script.

$ julia throwError.jl
ERROR: LoadError: a useful message
Stacktrace:
 [1] top-level scope at /path/throwError.jl:1
 [2] include(::Function, ::Module, ::String) at ./Base.jl:380
 [3] include(::Module, ::String) at ./Base.jl:368
 [4] exec_options(::Base.JLOptions) at ./client.jl:296
 [5] _start() at ./client.jl:506
in expression starting at /path/throwError.jl:1

I'm wondering why there is a LoadError: in my error message.

I get the same with other error types. For example:

throw(DivideError())
$ julia throwError.jl
ERROR: LoadError: DivideError: integer division error
stacktrace:
  ...same as above...

Upvotes: 6

Views: 1431

Answers (1)

ahnlabb
ahnlabb

Reputation: 2162

The full answer to the "Why?" In this question is fairly involved. One way to answer is to simply refer to the docstring for LoadError:

LoadError(file::AbstractString, line::Int, error)

An error occurred while includeing, requireing, or using a file. The error specifics should be available in the .error field.

So because the error happened while julia was trying to include the file and not when a function in the file was called after successful inclusion the error is wrapped in a LoadError.

But does Julia really have to wrap errors in LoadErrors? The answer seems to be no, as Julia's backtraces have improved, LoadErrors are no longer necessary to ensure that the developer has the necessary information about the location of the error. Since before 1.2 a PR that deprecates them has been pending. There are still some things to be figured out it seems but we'll hopefully be rid of them soon.

Other relevant links:

https://github.com/JuliaLang/julia/pull/31881 : Merged PR: Ensure syntax error locations appear in backtraces - improvement of backtraces for the purpose of enabling removal of LoadError.

https://github.com/JuliaLang/julia/issues/31830#issuecomment-487453858 : Comment explaining some of the background for the deprecation LoadError.

Upvotes: 5

Related Questions