Why could coroutines swallow exceptions?

I am just started to use coroutines and I like the concept and tools very much. However, even within my few weeks of practice, I ran into a problem twice.

In some circumstances, an uncaught exception thrown within a coroutine is simply swallowed and not printed out at all. I simply can’t find any cause justifying implicit hiding of an uncaught exception in any way.
I am sure there is a (either a design or a technical) reason for this, but I couldn’t have found out what it would be.

Also, as this is a language feature, there should be a solution to catch and print any uncaught exception in a coroutine architecture (without infesting any little code with try-catch), to avoid the above mentioned, hard to debug situation. I am looking forward the right way I should handle this!


1 Like

Can you provide some minimum repro where an exception is lost?

Also, take a look here for the details of how exceptions are dealt with: Exception Handling and Supervision

Maybe that can explain what you are seeing.

My initial guesses would be you are using async without await, or the exception is swallowed by your code or 3rd party code, or a global or thread level handler is swallowing the exception.

1 Like

Thanks for your answer.
I’ll try to set up an example, but our code is complex and when I try to strip it, the anomaly disappears. I’ve checked, that nothing catches and hides the exception. My guess is the problem is close to your async hint, but not calling async, but having wrong context hierrachy or non-blocking call which terminates the caller before the exception is caught.
I am new in the area of coroutines and just learning its ways. I may have used wrong structure, so I have to check all my previous usage.
However, I still hold my opinion, that an uncaught exception should never be lost in any circumstances, even with the cost of crashing the whole application if there is no other way to give a warning.