Can you pull a line number from a caught error?

Can you pull a line number from a caught error?
Im running multiple threads and when an error is found the Task dies, i was wondering if it is possible to get the line number and module or file from an error that has been caught?

stacktrace() gives you a vector of StackFrames which include the corresponding file and line number (but this probably gives you more information where the error is caught, rather than thrown)

1 Like

Sadly it does seem to be only where it is caught, maybe the data on where the error was is just lost

If you have the freedom to throw the errors yourself, you can cook up something like this:

struct MyException{T<:Exception} <: Exception
    err::T
    file::Symbol
    line::Int
end

function Base.showerror(io::IO, err::MyException)
    print(io, err.err)
end

macro myerror(err)
    return :( throw(MyException($(esc(err)),
                                $(QuoteNode(__source__.file)),
                                $(QuoteNode(__source__.line)))) )
end

function catch_error()
    try
        @myerror ErrorException("Error!")
    catch e
        if e isa MyException
            println("Caught error thrown at $(e.file):$(e.line)")
        end
    end
end
julia> catch_error()
Caught error thrown at /tmp/bar.jl:19

Line 19 is where the error is thrown.

BTW, there is catch_backtrace which should do exactly what you want, but you need to extract the information out of its pointers.