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

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:) (More Info)

Turns out I just got confused because I had two nested TaskFailedExceptions without realizing.

julia> t = @async 0//0;

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

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

seems totally fine.

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

That’s not particularly helpful, since

julia> typeof(t)

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

ok, but

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

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

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.

1 Like

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.

1 Like