如果使用这种格式:finally中的exceptions将不能被正确处理。
finally中将处理try中出错后的一些事情(如sql出错后,关闭数据库连接),而catch中将处理try和finally中的异常
- try {
- code that might throw exceptions
- } catch (Exception e) {
- show error dialog
- } finally {
- code that might throw exceptions
- }
如果使用另一种格式:
- try {
- try {
- code that might throw exceptions
- } finally {
- code that might throw exceptions
- }
- } catch (Exception e) {
- show error dialog
- }
本文探讨了Java中异常处理的最佳实践,特别是在try-catch-finally结构中的应用。通过对比两种不同的try-catch-finally用法,文章详细解释了如何有效处理try和finally块中可能出现的异常,并确保资源得到妥善清理。
2216

被折叠的 条评论
为什么被折叠?



