https://kotlinlang.org logo
Title
y

ylemoigne

10/09/2019, 8:33 AM
Another coroutine misunderstanding for me. I don't understand ; why
In 'uncaughtExceptionHandler' ...
is called ?
Then, how to prevent exception to be treated as
unhandled
?
e

Evan R.

10/09/2019, 12:44 PM
This is a guess, but check the last sentence on the documentation for
close()
(https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-send-channel/close.html):
Attempts to send or receive on a failed channel throw the specified cause exception.
It appears that your
produce<String>()
block will just move on to the next iteration after the
close()
and attempt to send, re-throwing the exception you closed the channel with. Because there’s no try/catch in the producer it’s being passed up to the CEH
I think adding a
break
in your if statement in the producer will fix the issue
y

ylemoigne

10/09/2019, 12:59 PM
Indeed. Thanks. (I still hope for better documentation as
produce
doc says
Uncaught exceptions in this coroutine close the channel with this exception as a cause and
 * the resulting channel becomes _failed_, so that any attempt to receive from such a channel throws exception.
which I understood as
throw Something()
in produce block is equivalent to calling
close(Something())
but the behavior are different
👍 1
e

Evan R.

10/09/2019, 1:47 PM
You might also test whether or not the following will hit the CEH:
I imagine the exception may have hit the CEH because the produce scope threw a second exception after the channel was already closed
y

ylemoigne

10/09/2019, 2:12 PM
With your example of doJob2, it hit CEH