关于try...catch...finally中return的疑惑
原文:http://www.cnblogs.com/and_he/archive/2012/04/17/2453703.html
关于try...catch...finally里面的return一直是面试的一个热门考点。无非就分以下几个情况:
1、当有finally语句并且try中有return,在执行到return(还未执行)的时候,会先执行finally里面的内容,然后再执行行try中的return。

package com.and.java.demo;
public class 测试 {
public static void main(String[] args) {
System.out.println(new 测试().test());
}
public String test() {
try {
System.out.println("try{...}");
return "try";
} catch (Exception e) {
System.out.println("catch{...}");
return "catch";
} finally {
System.out.println("finally{...}");
}
}
}

输出:
try{...}
finally{...}
try
2、在1的基础上,如果finally里面也有return语句,则try代码块中的return被屏蔽(不执行),即在try中遇到return的时候,会先执行finally里面的内容(包括finally里面的return语句)。

package com.and.java.demo;
public class 测试 {
public static void main(String[] args) {
System.out.println(new 测试().test());
}
public String test() {
try {
System.out.println("try{...}");
return "try";
} catch (Exception e) {
System.out.println("catch{...}");
return "catch";
} finally {
System.out.println("finally{...}");
return "finally";
}
}
}

输出:
try{...}
finally{...}
finally
遇到的问题:
这两种情况想必大家已经掌握。但是还有一种情况,也是我不能理解的地方。一般情况下,在finally里面作一些数据的关闭操作(比如文件,输入/输出流,数据库的关闭),试想一下,要是我们在finally里面对要返回的值进行修改,那会反应到最终的结果上去吗?(因为从上面的讲解可以知道,当try里面有return的时候,它不会立刻执行,会先执行finally里面的内容,然后再执行return)。

package com.and.java.demo;
public class 测试 {
public static void main(String[] args) {
System.out.println(new 测试().test());
}
public String test() {
String result = "";
try {
result = "try";
return result;
} catch (Exception e) {
result = "catch";
return result;
} finally {
result = "finally";
}
}
}

试想一下,它会输出"try" 呢还是"finally"呢?
输出:
try
确实只输出try,但是我们在finally里面是改变了result的值呀?
再进一步改进,判断finally里面的赋值语句是否执行

package com.and.java.demo;
public class 测试 {
public static void main(String[] args) {
System.out.println(new 测试().test());
}
public String test() {
String result = "";
try {
result = "try";
return result;
} catch (Exception e) {
result = "catch";
return result;
} finally {
System.out.println("t1->"+result);
result = "finally";
System.out.println("t2->"+result);
}
}
}

输出:
t1->try
t2->finally
try
从输出结果可以看出,finally里面的赋值语句是执行了的,但是在return结果中怎么就没变呢?(目前暂时研究到这个地步,仍没搞明白,还望各位高手指点)
关于try...catch...finally中return的疑惑的更多相关文章
- try catch finally中return的执行顺序
下面说一下try{ } catch{}中有return的情况 究竟是哪个return起作用的 话不多说 上代码 1 try中有return的情况 //普通方法 public static int hh ...
- try catch finally 中 returne的执行顺序
结论:1.不管有没有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...
- try catch finally 和return
结论:1.不管有木有出现异常,finally块中代码都会执行:2.当try和catch中有return时,finally仍然会执行:3.finally是在return后面的表达式运算后执行的(此时并没 ...
- Java异常处理中finally中的return会覆盖catch语句中的return语句
Java异常处理中finally中的return会覆盖catch语句中的return语句和throw语句,所以Java不建议在finally中使用return语句 此外 finally中的throw语 ...
- C#:在catch中return,会执行finally吗?
本文转自 vipxiaotian(CSDN) 请参考下面一段简单的语句块: 1: try2: {3: throw new Exception("new exception&qu ...
- 一个问题:关于finally中return吞掉catch块中抛出的异常
今天遇到一个感觉很神奇的问题,记录一下问题以及自己分析问题的思路. 预警:不知道怎么看java字节码的朋友可能需要先看一下如何阅读java字节码才能看懂后面的解释. 我有一段程序: public cl ...
- try catch finally 中包含return的几种情况,及返回结果
当当当,兴致勃勃的第二篇博客,散花~ 下面是正题(敲黑板) 第一种情况:在try和catch中有return,finally中没有return,且finally中没有对try或catch中要 retu ...
- 我敢说你不一定完全理解try 块,catch块,finally 块中return的执行顺序
大家好,今天我们来讲一个笔试和面试偶尔都会问到的问题,并且在工作中不知道原理,也会造成滥用. 大家可能都知道,try 块用来捕获异常,catch块是处理try块捕获的异常,finally 块是用来关闭 ...
- try catch finally中的return
try catch 中finally语句总是可以执行的,不管try中是否含有return语句 public class TestReturn { public static void main(Str ...
随机推荐
- 【STL】帮你复习STL泛型算法 一
STL泛型算法 #include <iostream> #include <vector> #include <algorithm> #include <it ...
- 某酒店2000W数据
某酒店2000W数据 2000万开房信息 [某酒店2000w数据 ct2000(解压密码:sjisauisa是就数据8很舒适好sjjss).rar] 国内安全漏洞监测平台乌云(WooYun.org)近 ...
- 叉积判断 POJ1696
// 叉积判断 POJ1696 #include <iostream> #include <algorithm> #include <cstring> #inclu ...
- Tkinter教程之Listbox篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811310 #Tkinter教程之Listbox篇#Listbox为列表框控件,它可以包含一个 ...
- leetcode—Swap Nodes in Pairs
1.题目描述 Given a linked list, swap every two adjacent nodes and return its head. For example, Given ...
- 基于easyui的验证扩展
基于easyui的验证扩展 ##前言 自己做项目也有好几年的时间了,一直没有时间整理自己的代码,趁春节比较闲,把自己以前的代码整理了一篇.这是基于easyui1.2.6的一些验证扩展,2012年就开始 ...
- MLE MAP EM
1.最大似然估计 (MLE): 什么是最大似然估计? 问题:给定一组观察数据还有一个参数待定的模型,如何来估计这个未知参数呢? 观察数据(x1,y1)......(xn,yn) 待定模型 ...
- linux查看端口信息以及关闭进程
lsof -i:6633 查看端口6633的使用情况 kill (+PID数值),结束进程
- 为Elasticsearch添加中文分词
Elasticsearch的中文分词很烂,所以我们需要安装ik.首先从github上下载项目,解压: cd /tmp wget https://github.com/medcl/elasticsear ...
- libyuv颜色空间转换开源库
libyuv据说在缩放和颜色空间转换,比ffmpeg效率高很多倍.不知道和我们的PP库比起来怎么样.同样有neon指令集优化.支持移动设备.