Java--finally
finally 子句(clause)是不是总会执行???
package com.volshell.test; public class Main {
public static void main(String[] args) {
change1();
} private static void change1(int word) {
System.out.println("测试结果:" + test());
} private static int test(int i) {
if (i == )
return ;
System.out.println("将要进入try块");
try {
System.out.println("try block");
return ;
} catch (Exception e) {
// TODO: handle exception
} finally {
System.out.println("finally");
i++;
return i;
}
}
}
上面结果为 测试结果:0
第一条:如果没有进入try中,是不会执行finally子句的。
package com.volshell.test; public class Main {
public static void main(String[] args) {
change1();
} private static void change1(int word) {
System.out.println("测试结果:" + test());
} private static int test(int i) {
// if (i == 1)
// return 0;
System.out.println("将要进入try块");
try {
System.out.println("try block");
System.exit(0);
return ;
} catch (Exception e) {
// TODO: handle exception
} finally {
System.out.println("finally");
i++;
return i;
}
}
}
测试结果:将要进入try块
try block
这次同样没有进入finally中。因为在try中调用了System.exit(0);
第二条:当一个线程正在执行try语句块或者catch语句块的时候,突然被打断或者终止,那么相应的finally是不会被执行的。
***********************************************************************************************************
The finally Block
The finally block always executes when the try block exits. This ensures that the finally
block is executed even if an unexpected exception occurs. But finally is useful for
more than just exception handling — it allows the programmer to avoid having cleanup
code accidentally bypassed by a return,continue, or break. Putting cleanup code in a
finally block is always a good practice, even when no exceptions are anticipated.
Note: If the JVM exits while the try or catch code is being executed, then the finally
block may not execute. Likewise, if the thread executing the try or catch code is
interrupted or killed, the finally block may not execute even though the application
as a whole continues.
***************************************************************************************************************
关于try,catch,finally的执行顺序的问题:
***************************************************************************************************************
where either at least one catch clause, or the finally clause, must be present. 至少有一个catch或者finally子句。可以没有catch
The body of the try statement is executed until either an exception is thrown or the body
finishes successfully. ---异常抛出或者程序正确执行都会执行try.
If an exception is thrown, each catch clause is examined in turn,
from first to last, to see whether the type of the exception object is assignable to
the type declared in the catch. When an assignable catch clause is found, its block
is executed with its identifier set to reference the exception object. No other catch
clause will be executed. Any number of catch clauses, including zero, can be associated
with a particular Try as long as each clause catches a different type of exception.
If no appropriate catch is found, the exception percolates (渗透)out of the try statement
into any outer try that might have a catch clause to handle it.--如果没有找到合适的捕获,交由外面的捕获来处理。
If a finally clause is present with a try, its code is executed after all other processing
in the try is complete. This happens no matter how completion was achieved, whether
normally, through an exception, or through a control flow statement such as return or
break.只有处理完毕try中的语句(不包括异常语句,return语句,break语句)之后,才会执行finally全部子句(包括其中的return子句).
***************************************************************************************************************
第三条:只有处理完毕try中的语句(不包括异常语句,return语句,break语句)之后,才会执行finally全部子句(包括其中的return子句等)。处理完finally之后再返回去处理try中的剩余子句。
Java--finally的更多相关文章
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题
背景起因: 记起以前的另一次也是关于内存的调优分享下 有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...
- Elasticsearch之java的基本操作一
摘要 接触ElasticSearch已经有一段了.在这期间,遇到很多问题,但在最后自己的不断探索下解决了这些问题.看到网上或多或少的都有一些介绍ElasticSearch相关知识的文档,但个人觉得 ...
- 论:开发者信仰之“天下IT是一家“(Java .NET篇)
比尔盖茨公认的IT界领军人物,打造了辉煌一时的PC时代. 2008年,史蒂夫鲍尔默接替了盖茨的工作,成为微软公司的总裁. 2013年他与微软做了最后的道别. 2013年以后,我才真正看到了微软的变化. ...
- 故障重现, JAVA进程内存不够时突然挂掉模拟
背景,服务器上的一个JAVA服务进程突然挂掉,查看产生了崩溃日志,如下: # Set larger code cache with -XX:ReservedCodeCacheSize= # This ...
- 死磕内存篇 --- JAVA进程和linux内存间的大小关系
运行个JAVA 用sleep去hold住 package org.hjb.test; public class TestOnly { public static void main(String[] ...
- 【小程序分享篇 一 】开发了个JAVA小程序, 用于清除内存卡或者U盘里的垃圾文件非常有用
有一种场景, 手机内存卡空间被用光了,但又不知道哪个文件占用了太大,一个个文件夹去找又太麻烦,所以我开发了个小程序把手机所有文件(包括路径下所有层次子文件夹下的文件)进行一个排序,这样你就可以找出哪个 ...
- Java多线程基础学习(二)
9. 线程安全/共享变量——同步 当多个线程用到同一个变量时,在修改值时存在同时修改的可能性,而此时该变量只能被赋值一次.这就会导致出现“线程安全”问题,这个被多个线程共用的变量称之为“共享变量”. ...
- Java多线程基础学习(一)
1. 创建线程 1.1 通过构造函数:public Thread(Runnable target, String name){} 或:public Thread(Runnable target ...
- c#与java的区别
经常有人问这种问题,用了些时间java之后,发现这俩玩意除了一小部分壳子长的还有能稍微凑合上,基本上没什么相似之处,可以说也就是马甲层面上的相似吧,还是比较短的马甲... 一般C#多用于业务系统的开发 ...
随机推荐
- php ajax提交数据 在本地可以执行,而在服务器不能执行
1.排除是服务器的问题 把单独的ajax项目传到服务器上,可以正常返回xml数据 2.排除是项目下的限制问题 把单独的ajax放在相应的项目文件夹下,单独访问该ajax发送数据的页面,能够正常执行 3 ...
- 几款Http小服务器
六款小巧的HTTP Server[C语言] http://blog.linuxphp.org/?action=show&id=60 转载:
- XML中SystemID和PublicID的区别
http://hi.baidu.com/binboot007/item/1533f91d52113d7c7b5f259c http://supportweb.cs.bham.ac.uk/documen ...
- Qt 继承QWidget setstylesheet解决
void myMainWidget::paintEvent(QPaintEvent * e) { QStyleOption opt; opt.init(this); QPainter p(this); ...
- CentOS 6.4下Squid代理服务器的安装与配置,反向代理
CentOS 6.4下Squid代理服务器的安装与配置 一.简介 代理服务器英文全称是Proxy Server,其功能就是代理网络用户去取得网络信息. Squid是一个缓存Internet 数据的软件 ...
- IPTABLES 映射问题
今天要做一个新的映射:将内网的一个8090口映射到外网的8087口. 在 /ETC/RC.LOCAL中最后插入: iptables -t nat -A PREROUTING -d outIP -p t ...
- 操蛋的UITableView重用机制
1,背景 你可能会遇见一下情况: 使用UITableView加载数据,比如你的每一个cell上面有一个UITextField,当你在第一个cell的UITextField中写下了内容,开始下滑UITa ...
- 查找jar包的站点
1.findJAR.com: http://www.findjar.com/index.x 2.jarfire: https://cn.jarfire.org/
- git从github下载代码
Github作为远程仓库的使用详解 http://blog.csdn.net/djl4104804/article/details/50778717 centos local: 通过g ...
- BZOJ 1800: [Ahoi2009]fly 飞行棋( 枚举 )
O(N2)算出有x条直径然后答案就是x(x-1)/2...这个数据范围是闹哪样! ----------------------------------------------------------- ...