Is there a better way of handling exceptions withi...
# coroutines
g
Is there a better way of handling exceptions within default values without manually creating loads of overloading methods?
Copy code
fun getDefault(): Any {
    check(false) { "failed"}
    TODO()
}

fun method(param: Any = getDefault()) = flow<Any> {
    TODO()
}
i.e avoiding loads of these when you have 2+ default values:
Copy code
fun method() = flow {
    method(getDefault())
        .collect {
            emit(it)
        }       
}
c
I don't really understand what you mean. Do you know about
@JvmOverloads
?
g
If you make a call to
method()
it will throw an exception the regular java way, where ideally you want exceptions to be handled within the async flow. To get the appropriate functionality I have to wrap the default value calls in another flow and create a function for every variation of parameters - what would normally be done automatically by
@JvmOverloads
I'm just looking for alternatives so you wouldn't have to wrap the call in two catch statements
Copy code
launch {
    try {
        method()
           .catch { it.printStackTrace() }
           .collect()
    } catch (e: Exception) {
        e.printStackTrace()
    }
}
c
You can do something like:
Copy code
fun foo(optionalParam: Bar? = null) = flow {
  val param = optionalParam ?: getDefault()
  emit(...)
}