# Can you pull a line number from a caught error?

**URL:** <https://discourse.julialang.org/t/can-you-pull-a-line-number-from-a-caught-error/61066>\
**Category:** New to Julia\
**Created:** [May 13, 2021, 8:07am UTC](https://discourse.julialang.org/t/can-you-pull-a-line-number-from-a-caught-error/61066 "2021-05-13T08:07:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ligert123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ligert123/32/24682_2.png) [@ligert123](https://discourse.julialang.org/u/ligert123)\
**Post date:** [May 13, 2021, 8:07am UTC](https://discourse.julialang.org/t/can-you-pull-a-line-number-from-a-caught-error/61066/1 "2021-05-13T08:07:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 13, 2021, 9:26am UTC](https://discourse.julialang.org/t/can-you-pull-a-line-number-from-a-caught-error/61066/2 "2021-05-13T09:26:23Z")

</div>

[`stacktrace()`](https://docs.julialang.org/en/v1/base/stacktraces/#Base.StackTraces.stacktrace) gives you a vector of [`StackFrame`](https://docs.julialang.org/en/v1/base/stacktraces/#Base.StackTraces.StackFrame)s which include the corresponding file and line number (but this probably gives you more information where the error is caught, rather than thrown)

---

<div class="post-metadata">

**Author:** ![ligert123](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ligert123/32/24682_2.png) [@ligert123](https://discourse.julialang.org/u/ligert123)\
**Post date:** [May 13, 2021, 11:05am UTC](https://discourse.julialang.org/t/can-you-pull-a-line-number-from-a-caught-error/61066/3 "2021-05-13T11:05:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 13, 2021, 11:24am UTC](https://discourse.julialang.org/t/can-you-pull-a-line-number-from-a-caught-error/61066/4 "2021-05-13T11:24:05Z")

</div>

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

```julia
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
julia> catch_error()
Caught error thrown at /tmp/bar.jl:19

```

Line 19 is where the error is thrown.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [May 13, 2021, 11:30am UTC](https://discourse.julialang.org/t/can-you-pull-a-line-number-from-a-caught-error/61066/5 "2021-05-13T11:30:21Z")

</div>

BTW, there is [`catch_backtrace`](https://docs.julialang.org/en/v1/base/base/#Base.catch_backtrace) which should do exactly what you want, but you need to extract the information out of its pointers.
