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#多用于业务系统的开发 ...
随机推荐
- pcap文件格式解析
pcap文件格式是常用的数据报存储格式,包括wireshark在内的主流抓包软件都可以生成这种格式的数据包 下面对这种格式的文件简单分析一下: pcap文件的格式为: 文件头 24字节 ...
- linux服务器安全小知识
使用单用户模式进入系统 Linux启动后出现boot:提示时,使用一个特殊的命令,如linuxsingle或linux 1,就能进入单用户模式(Single-User mode).这个命令非常有 ...
- 如何修改Protel99SE原理图的标题栏
本文主要讲述了如何修改Protel99SE原理图中的标题栏内容,使用者可以根据需要修改. 标题栏的格式: 1.添加模板:(1)菜单栏Design\Template\Set Template File ...
- HDU 3571 N-dimensional Sphere
高斯消元,今天数学死了无数次…… #include <cstdio> #include <cstring> #include <cmath> #include &l ...
- TCP/IP笔记 四.应用层(1)——DNS
1. DNS DNS(Domain Name System ):域名系统,是因特网的一项核心服务,它作为可以将域名和IP地址相互映射的一个分布式数据库,能够使人更方便的访问互联网,而不用去记住能够被机 ...
- poj1201 Intervals【差分约束+SPFA】
转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4303365.html ---by 墨染之樱花 题目链接:http://poj.org/pr ...
- SQL SERVER 2005 同步复制技术(转)
SQL SERVER 2005 同步复制技术 以下实现复制步骤(以快照复制为例) 运行平台SQL SERVER 2005 一.准备工作: 1.建立一个 WINDOWS 用户,设置为管理员权限,并设置密 ...
- CSS3线性渐变linear-gradient
转自 http://www.w3cplus.com/content/css3-gradient CSS3的线性渐变 一.线性渐变在Mozilla下的应用 -moz-linear-gradient( [ ...
- asp.net连接ORACLE数据库
这段时间维护客户的一个系统,该系统使用的是ORACLE数据库,之前开发的时候用的都是MSSQL,并没有使用过ORACLE.这两种数据库虽然都是关系型数据库,但是具体的操作大有不同,这里作下记录. 连接 ...
- IntelliJ IDEA 14 注册码生成器
IntelliJ IDEA 14 注册码生成器 文件为Java代码 自己编译运行里面的程序输入名称然后就生成注册码了工具:http://yun.baidu.com/s/1cZKsA部分工具生成的注册码 ...