首页 > 解决方案 > 将 Java 中的 try catch 重写为 Kotlin

问题描述

想把java try catch 改写成Kotlin。如果出现错误,只需要记录一个状态。将 Arrow 与 kotlin 一起使用是否过大?

Kotlin/Arrow 中的示例实现:

fun some_method(): <SomeReturnType>{
Try.invoke {
           //code which will return SomeReturnType
        }.getOrElse { throwable ->
            when (throwable) {
                is RuntimeException -> {
            //  do something
                }
                is SomeOtherEception -> {
            //  do something
                }
                else -> {do something}
            }
        }
}

标签: javakotlintry-catchinvokearrow-functions

解决方案


推荐阅读