# Is there a version of @sync that throws error when the task failed?

**URL:** <https://discourse.julialang.org/t/is-there-a-version-of-sync-that-throws-error-when-the-task-failed/36062>\
**Category:** General Usage\
**Created:** [March 16, 2020, 9:38pm UTC](https://discourse.julialang.org/t/is-there-a-version-of-sync-that-throws-error-when-the-task-failed/36062 "2020-03-16T21:38:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [March 16, 2020, 9:38pm UTC](https://discourse.julialang.org/t/is-there-a-version-of-sync-that-throws-error-when-the-task-failed/36062/1 "2020-03-16T21:38:41Z")

</div>

By default, `@sync` will collect all the exceptions in its block and throw them after everything is done. However, I have a code looking like

```julia
@sync begin
    @async for i in 1:1000
          # task A
     end
    @async for i in 1:1000
          # task B
    end
end

```

In my case, if task B failed, task A will be blocked and `@sync` will wait for ever.

Is there a version of `@sync` such that an exception is immediately thrown if one of the task it encloses failed?

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [March 17, 2020, 2:10am UTC](https://discourse.julialang.org/t/is-there-a-version-of-sync-that-throws-error-when-the-task-failed/36062/2 "2020-03-17T02:10:18Z")

</div>

Have you tried catching the exception in task B and using that information to end task A? Sync by default can’t do this unless you use `wait` on task B, which is another option.
