美文网首页
Kotlin异常处理(5)补充:典型异常面试题

Kotlin异常处理(5)补充:典型异常面试题

作者: 狼性代码人 | 来源:发表于2021-01-15 18:37 被阅读0次
    • finally不被执行情况【仅此一种情况下不执行】
    • finally中的代码块先于return语句执行
    • finally代码块虽然先于return执行,但不能修改return返回的局部变量值
    • finally代码块中建议不要是用return语句。

    一、finally不被执行情况【仅此一种情况下不执行】

    fun tryCatch1() {
        try {
            exitProcess(0)
        } catch (e: Exception) {
        } finally {
            println("Finally is running.")
        }
    }
    

    由于先执行了exitProcess(0)终止了程序,自然finally语句块不会被执行。

    二、finally中的代码块先于return语句执行

    fun tryCatch1(): Int {
        try {
            return 0
        } catch (e: Exception) {
            return 1
        } finally {
            println("Finally is running.")
        }
    }
    
    fun main() { tryCatch1().also(::print) }
    
    // 运行结果:
    Finally is running.
    0
    Process finished with exit code 0
    
    fun tryCatch1(): Int {
        try {
            throw IllegalStateException()
        } catch (e: Exception) {
            return 1
        } finally {
            println("Finally is running.")
        }
    }
    
    fun main() { tryCatch1().also(::println) }
    
    // 运行结果:
    Finally is running.
    1
    Process finished with exit code 0
    

    finally代码块先于 return语句catch代码块 执行。

    三、finally代码块虽然先于return执行,但不能修改return返回的局部变量值

    fun tryCatch1(): Int {
        var a = 0
        val b = 3
        try {
            return a + b
        } catch (e: Exception) {
            return a + b
        } finally {
            println("Finally is running.")
            a = 2
        }
    }
    
    fun main() { tryCatch1().also(::print) }
    
    // 运行结果:
    Finally is running.
    3
    Process finished with exit code 0
    

      finally代码块虽然在return语句之前执行【这里是指在return返回之前执行,即先执行 a+b,再执行finally代码块,再返回。】,但此时finally代码块不能通过重新给变量赋值的方式改变return语句的返回值。

    • 下面是一个易错理解案例。
    data class Person(var name: String)
    
    fun tryCatch1(): Person {
        val a = Person("张三")
        try {
            return a
        } catch (e: Exception) {
            return a
        } finally {
            println("Finally is running.")
            a.name = "李四"
        }
    }
    
    fun main() { tryCatch1().also(::print) }
    
    // 运行结果:
    Finally is running.
    Person1(name=李四)
    Process finished with exit code 0
    

      这里看似finally中的代码影响了return的返回值,但我们要理解return实际返回的是什么!!!(地址

    四、finally代码块中建议不要是用return语句。

    fun tryCatch1() {
        try {
            throw IllegalArgumentException()
        } catch (e: Exception) {
            println("Catch is running.")
            throw IllegalArgumentException("Catch throw exception.")
        } finally {
            return
        }
    }
    
    fun main() = Unit.also { try { tryCatch1() } catch (e: Exception) { e.message.also(::print) } }
    
    

    这段代码我们期望打印出的结果是:

    Catch is running.
    Catch throw exception.
    

    但实际运行结果是这样的:

    Catch is running.
    
    Process finished with exit code 0
    

    这是因为finally代码块先于throw执行,而finally中已经return,自然导致throw没有执行的机会。

    相关文章

      网友评论

          本文标题:Kotlin异常处理(5)补充:典型异常面试题

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