美文网首页
Kotlin 入门到进阶(26) -- 异常(1)

Kotlin 入门到进阶(26) -- 异常(1)

作者: 捉影T_T900 | 来源:发表于2025-02-12 17:26 被阅读0次
  • Kotlin 异常
    • Kotlin Java 异常的区别
      • Kotlin 异常是一个表达式
      • Kotlin 没有 checked exception
fun readFile(path: String) {
    throw FileNotFoundException("")
}

// 给 Java 调用,如果希望调用 try...catch,可以使用 @Throws 注解
// kotlin 没有 throws 关键字,只有 throw 关键字
@kotlin.jvm.Throws(IOException::class)
fun readFile2(path: String) {

}

fun main() {
    readFile("file")


    // try 是一个表达式
    val num = try {
        "".toInt()
    } catch (e: NumberFormatException) {
        -1
    }
}

相关文章

网友评论

      本文标题:Kotlin 入门到进阶(26) -- 异常(1)

      本文链接:https://www.haomeiwen.com/subject/ntjuujtx.html