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#多用于业务系统的开发 ...
随机推荐
- Githut Token (hidden): Githut 安装验证
登录https://github.com 进入https://github.com/settings/profile 参考 http://jingyan.baidu.com/article/22fe7 ...
- 4.PHP 教程_PHP 变量
PHP 变量 变量是用于存储信息的"容器": <?php $x = 5; $y = 6; $z = $x + $y; echo $z; ?> 与代数相似 x=5 y=6 ...
- bzoj 1040: [ZJOI2008]骑士 树形dp
题目链接 1040: [ZJOI2008]骑士 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3054 Solved: 1162[Submit][S ...
- Android系统智能指针的设计思路(轻量级指针、强指针、弱指针)
本博客为原创,转载请注明出处,谢谢. 参考博文:Android系统的智能指针(轻量级指针.强指针和弱指针)的实现原理分析 C++中最容易出错的地方莫过于指针了,指针问题主要有两类,一是内存泄露,二是无 ...
- 关于LD_DEBUG (转载)
引用 LD_DEBUGThe dynamic library loader used in linux (part of glibc) has some neat tricks. One of the ...
- CreateFile,ReadFile等API详解(或者说MSDN的翻译)
一.*****CreateFile***** 这个函数可以创建或打开一个对象的句柄,凭借此句柄就可以控制这些对象:控制台对象.通信资源对象.目录对象(只能打开).磁盘设备对象.文件对象.邮槽对象.管道 ...
- DropBox为什么一直那么红——靠用户体验,旗帜鲜明,它要保存的是你的重要随身资料,并且开放API
链接:http://www.zhihu.com/question/19705960/answer/71742127来源:知乎 看到这个问题竟然从11年答到现在,有趣的是这几年里国内云存储行业变化也是蛮 ...
- oracle 中的select ...connect by prior ...start with 及(+)的用法
1.select ...connect by prior ...start with的用法: select ... from <tablename> where <condition ...
- PHP 学习1- 函数之error_reporting(E_ALL ^ E_NOTICE)详细说明
在4.3.0中运行正常,在4.3.1中运行会提示Notice:Undefined varialbe:tmp_i 问题下下: 1.问题出在哪里? 2.应如何修改这段代码? 3.不改段代码,如何修改php ...
- JavaSript模块化 && AMD CMD 详解.....
模块化是指在解决某一个复杂问题或者一系列的杂糅问题时,依照一种分类的思维把问题进行系统性的分解以之处理.模块化是一种处理复杂系统分解为代码结构更合理,可维护性更高的可管理的模块的方式.可以想象一个巨大 ...