天天看点

try catch 扩展方法

fun CoroutineScope.tryLaunch(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend () -> Unit,
    catch: ((e: Exception) -> Unit)? = { Timber.w(it) }) {
  launch(context, start) {
    try {
      block()
    } catch (e: Exception) {
      catch?.invoke(e)
    }
  }
}

fun CoroutineScope.tryLaunch(
    context: CoroutineContext = EmptyCoroutineContext,
    start: CoroutineStart = CoroutineStart.DEFAULT,
    block: suspend () -> Unit) {
  tryLaunch(context = context,
      start = start,
      block = block,
      catch = {
        Timber.w(it)
      })
}
           
上一篇: 清明必看