I'm new to coroutines, and I'm curious which Dispatchers CoroutineExceptionHandler uses. I'd like to perform UI work, and display an Android SnackBar from the exception handler context. But, I want to confirm or not that the safety of doing so as I can't use withContext(Dispatchers.UI) to explicitly state the context. Thanks in advance!
I don't believe it uses any dispatchers, it just reports exceptions from whatever thread they were thrown on. You can always launch a coroutine from your handler if you need to ensure you're on the main thread.
The documentation should be more explicit though either way.
CoroutineExceptionHandler indeed can be called from any dispatcher in the Job hierarchy.
It means that if children of your UI scope use Dispatchers.Main and Dispatcher.Default, then CEH can be invoked only from Main or Default
Thank you both for the help! A follow up question: is CoroutineExceptionHandler only supposed to be used to catch CancellationException? I'm using coroutines with Retrofit and I'm having to catch network exceptions like timeout etc in a try catch block to prevent my presenter's Job from being canceled and it feels awkward.
@veganafro no, is it supposed to catch all unhandled exceptions (and it is not supposed to catch CancellationException at all because they are used for control flow)
I'm having to catch network exceptions like timeout etc in a try catch block to prevent my presenter's Job from being canceled and it feels awkward.
Probably SupervisorJob as your presenter job is what you are looking for