Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Which dispatchers does CoroutineExceptionHandler use? #1651

Closed
veganafro opened this issue Nov 8, 2019 · 4 comments
Closed

[Question] Which dispatchers does CoroutineExceptionHandler use? #1651

veganafro opened this issue Nov 8, 2019 · 4 comments
Labels
docs KDoc and API reference question

Comments

@veganafro
Copy link

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!

@zach-klippenstein
Copy link
Contributor

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.

@qwwdfsad
Copy link
Collaborator

qwwdfsad commented Nov 11, 2019

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

@veganafro
Copy link
Author

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.

@qwwdfsad
Copy link
Collaborator

@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

@qwwdfsad qwwdfsad added the docs KDoc and API reference label Nov 25, 2019
qwwdfsad added a commit that referenced this issue Nov 25, 2019
    * Clarifications of CoroutineExceptionHandler execution
    * Clarifications of MainCoroutineDispatcher.immediate
    * Outdated documentation (pointed out by Google AndoidX team) for isDispatchNeeded is rewritten
    *

Fixes #1650
Fixes #1651
Fixes #1634
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs KDoc and API reference question
Projects
None yet
Development

No branches or pull requests

3 participants