How to catch an exception inside a Job?

“`kt package org.example

import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Job import kotlinx.coroutines.launch

private var job: Job? = null

private suspend fun doSomething(f: suspend () -> Unit): Result<Unit> { return try { job = CoroutineScope(Dispatchers.IO).launch { f() } job?.join()

 Result.success(Unit) } catch (e: Exception) { Result.failure(e) } 

}

suspend fun main() {

val res = doSomething { println("hello") throw Exception("some error") } println("result: $res") 

} “`

output: hello Exception in thread "DefaultDispatcher-worker-1" java.lang.Exception: some error result: Success(kotlin.Unit)

I would expect res to be equal to Faillure(Exception(“somme error”)) instead of Success. How can i do that please?

submitted by /u/wiiznokes
[link] [comments]