Another coroutine misunderstanding for me. I don't...
# coroutines
y
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
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
Indeed. Thanks. (I still hope for better documentation as
produce
doc says
Copy code
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
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
With your example of doJob2, it hit CEH