try {}里有一个return语句,那么紧跟在这个try后的finally {}里的code会不会被执行,什么时候被执行,在return前还是后?
很多人都说是在return前执行,但我的写的测试程序确得到不同答案?有没有高手讲讲啊
public String message(){
System.out.println("returned");
return "123";
}
public String testFinal(){
try{
return message();
}catch(Exception e){
e.printStackTrace();
return "exception";
}finally{
System.out.println("do finally");
}
}
public static void main(String[] args){
Test test = new Test();
test.testFinal();
}
很多人都说是在return前执行,但我的写的测试程序确得到不同答案?有没有高手讲讲啊
public String message(){
System.out.println("returned");
return "123";
}
public String testFinal(){
try{
return message();
}catch(Exception e){
e.printStackTrace();
return "exception";
}finally{
System.out.println("do finally");
}
}
public static void main(String[] args){
Test test = new Test();
test.testFinal();
}
finally会在return之前执行如果在finally里面有return情况会更复杂
debug下,可以清楚看到。
楼主说到的 "finally会在return之前执行",小弟不认同,经测试过,
结果跟楼主的描述是相反的.结论是: 如果finally之前没有执行过:System.exit(0); 则 finally 是一定执行,并且是最后执行
System.out.println("returned");
return "123"; ---- 2
}public String testFinal(){
try{
return message(); ---- 1
}catch(Exception e){
e.printStackTrace();
return "exception";
}finally{
System.out.println("do finally"); --- 3
}
}public static void main(String[] args){
Test test = new Test();
test.testFinal();
}感觉结果应该是:
returned
do finally
返回到调用者那里的是finally中return的值;而如果finally中没有return,则理所当然的返回的是try或者catch中return的值,但是
finally中的代码是必须要执行的,而且是在return前执行,除非碰到exit()。
public String message(){
System.out.println("returned");
return "123"; ---- 2
}public String testFinal(){
try{
return message(); ---- 1
}catch(Exception e){
e.printStackTrace();
return "exception";
}finally{
System.out.println("do finally"); --- 3
}
}
public static void main(String[] args){
Test test = new Test();
test.testFinal();
} 土匪说的很对 finally 确实在return 前执行 在我标注的 2 的return 前执行
public String message() {
System.out.println("returned");
return "123";
} public String testFinal() {
try {
return message();
} catch (Exception e) {
e.printStackTrace();
return "exception";
} finally {
System.out.println("do finally");
}
} public static void main(String[] args) {
Test test = new Test();
System.out.println(test.testFinal());
}你吧程序改成 如上 就很清楚的看出 执行顺序了
public class Test2 {
public static void main(String[] args) {
System.out.print("in main b = " + tt());
} public static int tt() {
int b = 23;
try {
return b = 88;
} finally {
System.out.println("in finally b = " + b);
b = 66;
System.out.println("after being assigned, b = " + b);
return b = 44;
}
}
}
in finally b = 88
after being assigned, b = 66
in main b = 44
返回值和return(退出函数)确实是两回事
http://blog.csdn.net/zcjl/archive/2004/12/12/214123.aspx
public String testFinal(){
try{
return str;
}catch(Exception e){
e.printStackTrace();
return "exception";
}finally{
str += "0";
}
}public static void main(String[] args) { Test test = new Test();
System.out.println(test.testFinal());
}
为什么这句str += "0";执行了,打印是1呢?
http://blog.csdn.net/ZangXT/archive/2009/05/21/4206672.aspx
自己看一下吧。
规范就是规范。。如果try先有return,之后捕获到了异常转入catch后又有return。那执行那条??
finally是一定执行的,并且在return前执行!
但如果return是一个表达式的话,是先要执行这个表达式的。最后return只是说整个函数返回一个东西。然后函数退出。
*try中遇到return语句的话,先计算表达式的值,再执行finally,最后把刚才try中计算的值返回(注意,最后退出的时候不再重新计算)! */
public class Test { public static void main(String[] args) {
System.out.println(getPath());
System.out.println(getPath1());
System.out.println(getPath2());
}
public static String getPath() {
File f = new File("");
try {
int j = 3;
String path = "";
path = f.getAbsolutePath();
return f.getAbsolutePath();//try中遇到return语句的话,先计算表达式的值,在finally,最后把刚才try中计算的值返回(注意,最后退出返回的时候不再重新计算)!
} finally {
f = null;
}
} public static String getPath1() {
File f = new File("/");
try {
int j = 3;
String path = "";
path = f.getAbsolutePath();
String path2;
return path2 = path + "5555";
// int i=2;//error
} finally {
f = null;
}
} /**
* 从系统变量得到当前绝对路径
*
* @return
*/
public static String getPath2() {
return System.getProperty("user.dir");
} public static String getPath3() {
return Test.class.getResource("").toString();
} public static String getPath3_1() {
return Test.class.getResource("").getPath();
} public static String getPath4() {
return Test.class.getResource("/").toString();
} public static String getPath4_1() {
return Test.class.getResource("/").getPath();
} public static String getPath5() {
return Thread.currentThread().getContextClassLoader().getResource("")
.toString();
} public static String getPath5_1() {
return Thread.currentThread().getContextClassLoader().getResource("")
.getPath();
} public static String getPath6() {
return Thread.currentThread().getContextClassLoader().getResource("/")
.toString();
} public static String getPath6_1() {
return Thread.currentThread().getContextClassLoader().getResource("/")
.getPath();
}}
static private int x;
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.test());
System.out.println(test.test()); }
static int test()
{
try
{
return x;
}
finally
{
System.out.println("abc");
x++;
}
}
}
在return前执行