Kotlin嵌套try-catch块

我们还可以在需要时使用嵌套的try块。嵌套的try catch块就是将一个try catch块实现为另一个try块的块。
当代码块生成异常并且该代码块中的另一个代码语句也生成另一个异常时, 就会出现嵌套try catch块的要求。
嵌套try块的语法

.. try{// code block try{// code block }catch(e: SomeException){}}catch(e: SomeException){}..

Kotlin嵌套try块示例
fun main(args: Array< String> ) {val nume = intArrayOf(4, 8, 16, 32, 64, 128, 256, 512)val deno = intArrayOf(2, 0, 4, 4, 0, 8)try {for (i in nume.indices) {try {println(nume[i].toString() + " / " + deno[i] + " is " + nume[i] / deno[i])} catch (exc: ArithmeticException) {println("Can't divided by Zero!")}}} catch (exc: ArrayIndexOutOfBoundsException) {println("Element not found.")}}

【Kotlin嵌套try-catch块】输出:
4 / 2 is 2Can't divided by Zero!16 / 4 is 432 / 4 is 8Can't divided by Zero!128 / 8 is 16Element not found.

    推荐阅读