https://kotlinlang.org logo
#coroutines
Title
# coroutines
a

addamsson

04/23/2019, 6:59 AM
I'm trying to write a fire-and-forget service which offloads jobs to worker threads am I doing it the right way? I don't want the
Test
service to fail even if the
Job
fails because each
Job
is independent, I just want the error handling.
Copy code
class Test : CoroutineScope {

    override val coroutineContext = Dispatchers.Default + SupervisorJob()

    fun doSomething(): Job {
        val handler = CoroutineExceptionHandler { _, exception ->
            // log error
        }
        return launch(handler) {
            // do things
        }
    }
}
@gildor do you have any idea about this?
g

gildor

04/24/2019, 10:21 AM
Looks fine, but I would handle error with try/catch in
// do things
, but it depends on case of course
a

addamsson

04/24/2019, 10:50 AM
thanks!
I added a
handler
because it might happen that an exception is thrown in the
catch
block
and that leads to missed errors
if I understand the concept correctly
g

gildor

04/24/2019, 12:44 PM
Isn't better to handle than exception in catch block? It just looks for me more explicitl than exception handler
a

addamsson

04/24/2019, 1:55 PM
it is
i'm just not 100% sure what would happen if an exception is thrown in the catch block
5 Views