Get error string contents

How do you access to the error string payload in addition to the error type?

For example:

d = Dict()
e = try
  a = parse(Float64, d["a"])
catch error
  error
end

For convenience in REPL, e is set to the error, which is KeyError("a").

I can get the error type of KeyError with typeof(e), but I don’t know how to get the string payload of "a" from this error.

julia> dump(KeyError("a"))
KeyError
  key: String "a"

julia> KeyError("a").key
"a"

Note that different exception types can have different struct field names, however.

1 Like

Also recently found:

julia> fieldnames(KeyError)
(:key,)

This file is another good reference on displaying errors.
https://github.com/JuliaLang/julia/blob/a18ab97f74dce6f9d116690bff89ddd73c0ce308/base/errorshow.jl#L138