# How do I see errors encountered in async coroutines/tasks?

**URL:** https://discourse.julialang.org/t/how-do-i-see-errors-encountered-in-async-coroutines-tasks/44134
**Category:** General Usage
**Tags:** question
**Created:** [August 2, 2020, 2:30pm UTC](https://discourse.julialang.org/t/how-do-i-see-errors-encountered-in-async-coroutines-tasks/44134 "2020-08-02T14:30:00Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![coldinjection](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/coldinjection/32/51061_2.png) [@coldinjection](https://discourse.julialang.org/u/coldinjection)
#### Post date: [August 2, 2020, 2:30pm UTC](https://discourse.julialang.org/t/how-do-i-see-errors-encountered-in-async-coroutines-tasks/44134/1 "2020-08-02T14:30:00Z")

</div>

There will be no error message in the RPEL if an error is encountered in an async task, unless you check the state of the task. I find it super difficult to detect errors in async tasks when the tasks are not in the top level scope.  
Example:

```julia
function f()
    wait(Timer(1))
    undeclaredvar
end
function g()
    @async f()
    nothing
end
g()
# there will be no way to find out the error about undeclaredvar

```

Is there a way to print out the error message as soon as an error happens in a task?

---

<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: [August 2, 2020, 4:10pm UTC](https://discourse.julialang.org/t/how-do-i-see-errors-encountered-in-async-coroutines-tasks/44134/2 "2020-08-02T16:10:35Z")

</div>

You can do it like that:

```julia
function f()
    wait(Timer(1))
    undeclaredvar
end

function g()
    return @async f()
end

julia> t = g()
Task (runnable) @0x0000000111b10010

julia> t
Task (failed) @0x0000000111b10010
UndefVarError: undeclaredvar not defined
f at /Users/paul/.julia/dev/tmp/test.jl:4 [inlined]
(::var"#9#10")() at ./task.jl:358

```

If you have a task variable, you can see the task status or – if it failed – the stack trace.

---

<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: [August 2, 2020, 4:27pm UTC](https://discourse.julialang.org/t/how-do-i-see-errors-encountered-in-async-coroutines-tasks/44134/3 "2020-08-02T16:27:41Z")

</div>

or – after rereading your question 🙂

```julia
function f()
	try
    	wait(Timer(1))
		undeclaredvar
	catch exc
		println(stacktrace())
		println(exc)
	end
end

function g()
	@async f()
	nothing
end

julia> g()

julia> Base.StackTraces.StackFrame[f() at test.jl:7, (::var"#11#12")() at task.jl:358]
UndefVarError(:undeclaredvar)
julia> 

julia>

```

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [February 2, 2023, 5:48am UTC](https://discourse.julialang.org/t/how-do-i-see-errors-encountered-in-async-coroutines-tasks/44134/4 "2023-02-02T05:48:27Z")

</div>

See also [this related topic](https://discourse.julialang.org/t/how-to-get-backtrace-of-async-task/18170/2).
