在Sun Tutorial中有这样一句话:The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. 看来finally块中的语句应该是总会执行的. 先来写一个最常见的写法: public class finally_test { public static v
大家好,今天我们来讲一个笔试和面试偶尔都会问到的问题,并且在工作中不知道原理,也会造成滥用. 大家可能都知道,try 块用来捕获异常,catch块是处理try块捕获的异常,finally 块是用来关闭资源.一个try块后面可以跟多个catch块,如果后面一个catch块也不跟,就一定要跟一个finally 块. 结论1:当在try块遇到return语句时,finally语句块将在方法返回之前被执行,但是返回值不受finally块中重新赋值的影响. public class FinallyTest
在finally中使用try/catch,并且catch的时候抛出异常 IDEA会提示警告 Reports throw statements inside of finally blocks. While occasionally intended, such throw statements may mask exceptions thrown, and tremendously complicate debugging. 大意是:这样可能会掩盖异常抛出 做以下测试代码: public sta