I'm trying to write a fire-and-forget service whic...
# coroutines
a
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
Looks fine, but I would handle error with try/catch in
// do things
, but it depends on case of course
a
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
Isn't better to handle than exception in catch block? It just looks for me more explicitl than exception handler
a
it is
i'm just not 100% sure what would happen if an exception is thrown in the catch block