为什么要在捕获后最终使用而不是代码 [英] Why use finally instead of code after catch

查看:72
本文介绍了为什么要在捕获后最终使用而不是代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这样做

} catch (SQLException sqle) {
    sqle.printStackTrace();
} finally {
    cs.close();
    rs.close();
}

代替这个

} catch (SQLException sqle) {
    sqle.printStackTrace();
}
rs.close();
cs.close();

推荐答案

因为如果抛出异常,则在try块执行完后没有代码除非被抓到.不管try块内部发生什么,始终执行finally块.

Because if an exception gets thrown no code after the try block is executed unless the exception is caught. A finally block is always executed no matter what happens inside your try block.

这篇关于为什么要在捕获后最终使用而不是代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆