# Rethrow error from @async

**URL:** <https://discourse.julialang.org/t/rethrow-error-from-async/48609>\
**Category:** General Usage\
**Created:** [October 19, 2020, 7:12am UTC](https://discourse.julialang.org/t/rethrow-error-from-async/48609 "2020-10-19T07:12:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kobied](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobied/32/16432_2.png) [@kobied](https://discourse.julialang.org/u/kobied)\
**Post date:** [October 19, 2020, 7:12am UTC](https://discourse.julialang.org/t/rethrow-error-from-async/48609/1 "2020-10-19T07:12:03Z")

</div>

hello,  
I need help understanding why the rethrow from an @async does not work.  
my test code is as follow:

```julia
bad_fun1() = error("this is error from bad_fun1")

function rethrow_fun1()
    try
        bad_fun1()
    catch e
        println("got error from bad_fun1, retrow to rethrow_fun1")
        rethrow(e)
    end
end

function main()
    try
        @async begin
            try
                rethrow_fun1() 
            catch
                println("got error from rethrow_fun1 at @async , rethrow to main")
                rethrow(e)
            end
        end
    catch e
        println("got error from rethrow_fun1, retrow to main")
        rethrow(e)
    end
end

try
    main()
catch e
    println("got error from main")
end

```

the output of this code is:

```julia
got error from bad_fun1, retrow to rethrow_fun1
got error from rethrow_fun1 at @async , rethrow to main

```

I expect to get the error all the way till the last try - catch but the rethrow from with in the @async does not seem to work…  
Thanks in ahead for any help in the subject.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 19, 2020, 7:25am UTC](https://discourse.julialang.org/t/rethrow-error-from-async/48609/2 "2020-10-19T07:25:58Z")

</div>

You should wait on the task you created, either by `@sync` or by explicit `wait`. For example

```julia
function main()
    @sync begin
        try
            @async begin
                try
                    rethrow_fun1()
                catch
                    println("got error from rethrow_fun1 at @async , rethrow to main")
                    rethrow(e)
                end
            end
        catch e
            println("got error from rethrow_fun1, retrow to main")
            rethrow(e)
        end
    end
end

```

```julia
julia> try
           main()
       catch e
           println("got error from main")
       end
got error from bad_fun1, retrow to rethrow_fun1
got error from rethrow_fun1 at @async , rethrow to main
got error from main

```

---

<div class="post-metadata">

**Author:** ![kobied](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kobied/32/16432_2.png) [@kobied](https://discourse.julialang.org/u/kobied)\
**Post date:** [October 19, 2020, 7:40am UTC](https://discourse.julialang.org/t/rethrow-error-from-async/48609/3 "2020-10-19T07:40:16Z")

</div>

Thank you for the supper quick reply.  
I’ll implement it in my code.  
Can you elaborate alittle more about the logic for doing so?  
It will help me avoid similar problams in the future.  
Thank again.

---

<div class="post-metadata">

**Author:** ![zgornel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zgornel/32/217487_2.png) [@zgornel](https://discourse.julialang.org/u/zgornel)\
**Post date:** [October 19, 2020, 9:38am UTC](https://discourse.julialang.org/t/rethrow-error-from-async/48609/4 "2020-10-19T09:38:48Z")

</div>

`@sync` catches async thrown exceptions

```julia
@sync

  Wait until all lexically-enclosed uses of @async, @spawn, @spawnat and @distributed are complete. All exceptions thrown by enclosed async operations are collected and thrown as a CompositeException.

```
