# How to find the error line using try/catch statements?

**URL:** <https://discourse.julialang.org/t/how-to-find-the-error-line-using-try-catch-statements/65511>\
**Category:** General Usage\
**Tags:** error, exception\
**Created:** [July 29, 2021, 6:05pm UTC](https://discourse.julialang.org/t/how-to-find-the-error-line-using-try-catch-statements/65511 "2021-07-29T18:05:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Mena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastian_mena/32/22819_2.png) [@Sebastian\_Mena](https://discourse.julialang.org/u/Sebastian_Mena)\
**Post date:** [July 29, 2021, 6:05pm UTC](https://discourse.julialang.org/t/how-to-find-the-error-line-using-try-catch-statements/65511/1 "2021-07-29T18:05:57Z")

</div>

Hi,  
I want to get the line number of an error using try/catch statements.

```julia
try
[my_function]

catch err
println(err) #this only shows the name of the error
#i want to print the line number of the error

```

I hope you can help me.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 29, 2021, 6:22pm UTC](https://discourse.julialang.org/t/how-to-find-the-error-line-using-try-catch-statements/65511/2 "2021-07-29T18:22:29Z")

</div>

You want `rethrow`

```julia
julia> function foo()
           throw(error("hello"))
       end;

julia> try
           foo()
       catch e
           println("foo threw an error")
           rethrow(e)
       end
foo threw an error
ERROR: hello
Stacktrace:
 [1] error(s::String)
   @ Base .\error.jl:33
 [2] foo()
   @ Main .\REPL[33]:2
 [3] top-level scope
   @ REPL[34]:2

```

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [July 29, 2021, 6:30pm UTC](https://discourse.julialang.org/t/how-to-find-the-error-line-using-try-catch-statements/65511/3 "2021-07-29T18:30:11Z")

</div>

If you do not want to rethrow error, then you can use `catch_backtrace()` with any of logger macros

```julia
julia> try
           throw(error("hello"))
       catch err
           @error "ERROR: " exception=(err, catch_backtrace())
       end
┌ Error: ERROR:
│ exception =
│ hello
│ Stacktrace:
│ [1] Base ./client.jl:485
│ _start()
│ [2] Base ./client.jl:302
│ exec_options(opts::Base.JLOptions)
│ [3] Base ./client.jl:372
│ run_main_repl(interactive::Bool, quiet::Bool, banner::Bool, history_file::Bool, color_set::Bool)
│ [4] ./essentials.jl:706 [inlined]
│ invokelatest
...

```

---

<div class="post-metadata">

**Author:** ![Sebastian\_Mena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastian_mena/32/22819_2.png) [@Sebastian\_Mena](https://discourse.julialang.org/u/Sebastian_Mena)\
**Post date:** [July 29, 2021, 9:17pm UTC](https://discourse.julialang.org/t/how-to-find-the-error-line-using-try-catch-statements/65511/4 "2021-07-29T21:17:50Z")

</div>

Thanks @pdeffebach, your solution does work.  
Do you know how to try with another option when first doesn’t work?  
For example:

```julia
try
var = first_option #this variable sometimes works, but in some cases throw an error

#so, when this var throw an error, I want to do other thing.
"other option":
var = second_option

```

In others words, i want try with an option, but if this option throw me an error, i want to do the second option, that I know its work. I hope you understand me.

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [July 29, 2021, 9:41pm UTC](https://discourse.julialang.org/t/how-to-find-the-error-line-using-try-catch-statements/65511/5 "2021-07-29T21:41:01Z")

</div>

Have you tried a simple `if` condition?

---

<div class="post-metadata">

**Author:** ![Sebastian\_Mena](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sebastian_mena/32/22819_2.png) [@Sebastian\_Mena](https://discourse.julialang.org/u/Sebastian_Mena)\
**Post date:** [July 29, 2021, 9:49pm UTC](https://discourse.julialang.org/t/how-to-find-the-error-line-using-try-catch-statements/65511/6 "2021-07-29T21:49:44Z")

</div>

I tought do that, but I don’t know how to put it in the code.  
It’s something like this?

```julia
if first_option == error
      var = second_option
else
      var = first_option

```

Is there any “is error” function?  
How would you do it?

---

<div class="post-metadata">

**Author:** ![ptoche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ptoche/32/23554_2.png) [@ptoche](https://discourse.julialang.org/u/ptoche)\
**Post date:** [July 29, 2021, 10:03pm UTC](https://discourse.julialang.org/t/how-to-find-the-error-line-using-try-catch-statements/65511/7 "2021-07-29T22:03:16Z")

</div>

Something like this may or may not be what you’re after: difficult to tell without context.

The other thing is since you double-posted, perhaps you could delete the other post.

```
function foo1()
    throw(error("first error caught!"))
end

function foo2()
    throw(error("second error caught!"))
end

try
    foo1()
catch e
    print("foo1 just threw an error! will try foo2\n")
    try 
        foo2()
    catch e
        print("oops! foo2 also threw an error!!")
        rethrow(e)
    end
end

```
