Java的try-catch-finally
如下引用文章:https://help.semmle.com/wiki/display/JAVA/Finally+block+may+not+complete+normally
A finally block that does not complete normally suppresses any exceptions that may have been thrown in the corresponding try block. This can happen if the finally block contains any return or throw statements, or if it contains any break or continue statements whose jump target lies outside of the finally block.
Recommendation
To avoid suppressing exceptions that are thrown in a try block, design the code so that the corresponding finally block always completes normally. Remove any of the following statements that may cause it to terminate abnormally:
returnthrowbreakcontinue
举例子:
try {}
catch (Exception e) {}
finally {
throw new Exception() ;
}
try {}
catch (Exception e) {}
finally {
return;
}
L:
try {}
catch (Exception e) {}
finally {
break L;
}
try {}
catch (Exception e) {}
finally {
try{
throw new Exception() ;
}catch(Exception e){
}finally{
return;
}
}
都会出现警告信息finally block does not complete normally,如下则不会:
try {}
catch (Exception e) {}
finally {
try {
throw new Exception();
} catch (Exception e) {
}
}
try {}
catch (Exception e) {}
finally {
L: {
break L;
}
}
关于try-catch-finally的执行流程图如下:

Java的try-catch-finally的更多相关文章
- 【转】Java中try catch finally语句中含有return语句的执行情况(总结版)
Java中try catch finally语句中含有return语句的执行情况(总结版) 有一点可以肯定,finally块中的内容会先于try中的return语句执行,如果finall语句块中也有r ...
- Java 异常处理 try catch finally throws throw 的使用和解读(一)
//最近的一个内部表决系统开发过程中,//发现对异常处理还存在一些模棱两可的地方,//所以想着整理一下//主要涉及到://1.try catch finally throws throw 的使用和解读 ...
- JEECG&JWT异常捕获强化处理 | Java: Meaning of catch (final SomeException e)?
//从header中得到token String authHeader = request.getHeader(JwtConstants.AUTHORIZATION); if (authHeader ...
- Java中try,catch,finally的用法
Java中try,catch,finally的用法,以前感觉还算熟悉,但看到一篇博文才有更深点的理解,总结网友博客如下. Java异常处理的组合方式: 1.try+catch 运行流程:运行到try ...
- java中 try catch finally和return联合使用时,代码执行顺序的小细节
代码1测试 public static void main(String[] args) { aa(); } static int aa() { try { int a=4/0; } catch (E ...
- java异常处理try catch finally
1 异常 1.1 异常处理的作用 在编程时,如果出现文件打开失败,读写文件就会异常退出.如果出现内存溢出错误,程序也会异常退出.如果不能对这些异常进行处理.程序则无法正常运行.所 ...
- Java try和catch的使用介绍
尽管由Java运行时系统提供的默认异常处理程序对于调试是很有用的,但通常你希望自己处理异常.这样做有两个好处.第一,它允许你修正错误.第二,它防止程序自动终止.大多数用户对于在程序终止运行和在无论何时 ...
- Java中try catch finally语句中含有return语句的执行情况(总结版)
在这里看到了try >但有一点是可以肯定的,finally块中的内容会先于try中的return语句执行,如果finall语句块中也有return语句的话,那么直接从finally中返回了,这也 ...
- Java如何使用catch来处理异常?
在Java编程中,如何使用catch块来处理异常? 此示例显示如何使用catch来处理异常. package com.yiibai; public class UseOfCatch { public ...
- 深入剖析java的try…catch…finally语句
一.前言 前些天参加面试的时候有一道题: public class test { public static void main(String[] args){ try { return; } fin ...
随机推荐
- Swift的Optional类型
我们使用Swift这个苹果新推出的编程语言已经有一段时间了.其中的一个极大的优点就是苹果称为“optional types”的东西.几乎所有的objective-c程序员都知道用nil来表示某个引用类 ...
- java 泛型的类型擦除和桥方法
oracle原文地址:https://docs.oracle.com/javase/tutorial/java/generics/erasure.html 在Java中,泛型的引入是为了在编译时提供强 ...
- Feed back TFS 2017 RC upgrade status to product team in product group 2017.03.01
作为微软的MVP,有一个我最喜欢的好处,就是可以与产品组(产品研发部门)有零距离接触,可以最先拿到即将发版的产品,并且和产品组沟通,对产品中出现的问题实时反馈. 看到TFS产品组吸收了自己的建议和反馈 ...
- Python 数据结构与算法——链表
#构造节点类 class Node(object): def __init__(self,data=None,_next=None): ''' self.data:为自定义的数据 self.next: ...
- TextBox 加阴影
<Border.Effect> <DropShadowEffect x:Name="dse" BlurRadius="8" ShadowDep ...
- 手写数字识别---demo
数据准备 课程中获取数据的方法是从库中直接load_data from keras.datasets import mnist (x_train, y_train), (x_test, y_test) ...
- 792. Number of Matching Subsequences
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...
- 栈的实现——java
和C++一样,JDK包中也提供了"栈"的实现,它就是集合框架中的Stack类.关于Stack类的原理,在"Java 集合系列07之 Stack详细介绍(源码解析)和使用示 ...
- ThinkPHP5.0手把手实现手机阿里云短信验证
阿里云短信服务介绍阿里云短信服务就是以前的阿里大于,不过现在融合得到阿里云平台了.首先,你需要注册一个阿里云账号,这个自行解决. 仅用于测试使用官方送的代金券够用了.相关配置1.开通阿里云Access ...
- jvm(1)类的加载(三)(线程上下文加载器)
简介: 类加载器从 JDK 1.0 就出现了,最初是为了满足 Java Applet 的需要而开发出来的. Java Applet 需要从远程下载 Java 类文件到浏览器中并执行. 现在类加载器在 ...