# What's the right way to unwrap a \`TaskFailedException\`?

**URL:** https://discourse.julialang.org/t/whats-the-right-way-to-unwrap-a-taskfailedexception/55052
**Category:** General Usage
**Created:** [February 11, 2021, 10:02am UTC](https://discourse.julialang.org/t/whats-the-right-way-to-unwrap-a-taskfailedexception/55052 "2021-02-11T10:02:53Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)
#### Post date: [February 11, 2021, 10:02am UTC](https://discourse.julialang.org/t/whats-the-right-way-to-unwrap-a-taskfailedexception/55052/1 "2021-02-11T10:02:53Z")

</div>

What’s the right way to unwrap a `TaskFailedException`? `err.task.exception.task.exception` gives me the original exception, but that seems very brittle.

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1613037732039500?thread_ts=1613037732.039500&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [February 11, 2021, 10:18am UTC](https://discourse.julialang.org/t/whats-the-right-way-to-unwrap-a-taskfailedexception/55052/2 "2021-02-11T10:18:29Z")

</div>

Turns out I just got confused because I had two nested `TaskFailedException`s without realizing.

```julia
julia> t = @async 0//0;

julia> err = try
       wait(t)
       catch err
       err
       end
TaskFailedException(Task (failed) @0x00007f886e84bd00)

julia> err.task.exception
ArgumentError("invalid rational: zero(Int64)//zero(Int64)")

```

seems totally fine.

---

<div class="post-metadata">

### Author: ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)
#### Post date: [February 11, 2021, 10:23am UTC](https://discourse.julialang.org/t/whats-the-right-way-to-unwrap-a-taskfailedexception/55052/3 "2021-02-11T10:23:38Z")

</div>

> [@pfitzseb](#):
>
> `t = @async 0//0;`

```julia
julia> t
Task (failed) @0x000000011347f310
ArgumentError: invalid rational: zero(Int64)//zero(Int64)

```

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [February 11, 2021, 10:25am UTC](https://discourse.julialang.org/t/whats-the-right-way-to-unwrap-a-taskfailedexception/55052/4 "2021-02-11T10:25:20Z")

</div>

That’s not particularly helpful, since

```julia
julia> typeof(t)
Task

```

So while this is pretty printed I can’t actually do anything with it.

---

<div class="post-metadata">

### Author: ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)
#### Post date: [February 11, 2021, 10:27am UTC](https://discourse.julialang.org/t/whats-the-right-way-to-unwrap-a-taskfailedexception/55052/5 "2021-02-11T10:27:21Z")

</div>

ok, but

```julia
julia> t.result
ArgumentError("invalid rational: zero(Int64)//zero(Int64)")

```

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [February 11, 2021, 10:31am UTC](https://discourse.julialang.org/t/whats-the-right-way-to-unwrap-a-taskfailedexception/55052/6 "2021-02-11T10:31:10Z")

</div>

Sure, that’s basically what I wrote above, no? Except with `exception` instead of `result`.

---

<div class="post-metadata">

### Author: ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)
#### Post date: [February 11, 2021, 10:37am UTC](https://discourse.julialang.org/t/whats-the-right-way-to-unwrap-a-taskfailedexception/55052/7 "2021-02-11T10:37:44Z")

</div>

I wanted only to point out, that having the task variable `t` or a channel – if it happens to be bound to a channel –, you have direct access to the exception without wrapping it in a `try - catch` block. I use it all the time.

---

<div class="post-metadata">

### Author: ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)
#### Post date: [February 11, 2021, 10:42am UTC](https://discourse.julialang.org/t/whats-the-right-way-to-unwrap-a-taskfailedexception/55052/8 "2021-02-11T10:42:25Z")

</div>

Right, makes sense.  
This did indeed come up in the context of iterating over a `Channel` (returned from `walkdir`), but it seems easier to look at the caught exception than accessing the channel directly to me.
