try catch finally,try里有return,finally还执行么?
答案: 执行,并且返回return时,finally的执行早于try。
try-catch-finally的执行顺序
无return
当try中的t()没有抛出异常
public static void main(String[] args) {
try{
System.out.println("try");
}catch (Exception e) {
System.out.println("catch");
}finally {
System.out.println("finally");
}
System.out.println("other");
}
执行结果:
try
finally
other
Process finished with exit code 0
因为没有捕捉到异常,那么执行try而不会执行catch,而finally无论如何都要执行。
其执行顺序为:try--catch--other
当try中有异常时
/**
* @author gf
* @date 2023/2/17
*/
public class TryCatchFinally {
public static void main(String[] args) {
try{
System.out.println("try");
int i = 1 / 0;
}catch (Exception e) {
System.out.println("catch");
}finally {
System.out.println("finally");
}
}
}
运行结果:
try
catch
finally
other
Process finished with exit code 0
当 try中抛出异常,那么抛出异常的语句之后的代码, 程序会尝试捕捉异常。捕捉Exception,捕捉成功,执行 catch;一旦捕捉到一个异常,不会再尝试捕捉其他异常,直接执行finally里的代码;最后再执行后面的其他代码。
其执行顺序是:try--catch--finally--other
有return
try块中有return
public static void main(String[] args) {
System.out.println(test());
}
public static String test() {
try {
return "return--try";
} catch (Exception e) {
System.out.println("catch");
} finally {
System.out.println("finally");
}
return "other";
}
执行结果:
finally
return--try
Process finished with exit code 0
程序执行try块中return之前(包括return语句中的表达式运算)代码;再执行finally块,最后执行try中return;finally块之后的语句return不再执行,因为程序在try中已经return过了。
其执行顺序是:finally--return(try)
catch块中有return
public static void main(String[] args) {
System.out.println(test());
}
public static String test() {
try {
int i=10/0;
} catch (Exception e) {
return "return--catch";
} finally {
System.out.println("finally");
}
return "other";
}
执行结果:
finally
return--catch
Process finished with exit code 0
有异常:执行catch中return之前(包括return语句中的表达式运算)代码,再执行finally语句中全部代码,最后执行catch块中return。 finally之后的return不再执行。
无异常:执行完try再finally再return。
try块和finally块中有return
public static void main(String[] args) {
System.out.println(test());
}
public static String test() {
try {
return "try";
} catch (Exception e) {
// return "return--catch";
System.out.println("return--catch");
} finally {
return "finally";
}
}
指向结果;
finally
Process finished with exit code 0
无异常:程序执行try块中return之前(包括return语句中的表达式运算)代码;再执行finally块,因为finally块中有return所以提前退出,而不再执行try中的return;
有异常:不执行try,顺序执行catch-finally
结论:得到finally中的返回值finally。
catch块和finally块中有return
public static void main(String[] args) {
System.out.println(test());
}
public static String test() {
try {
// try中有异常 catch和finally中有return
int i=10/0;
} catch (Exception e) {
return "return--catch";
} finally {
return "finally";
}
}
执行结果:
finally
Process finished with exit code 0
无异常:执行try后跳过catch执行finally;得到finally的返回值finally;
有异常:程序执行catch块中return之前(包括return语句中的表达式运算)代码;再执行finally块,因为finally块中有return所以提前退出。而不再执行catch中的return。
结论:得到finally中的返回值finally。
try块、catch块和finally块中有return
public static void main(String[] args) {
System.out.println(test());
}
public static String test() {
try {
int i=10/0;
return "try";
} catch (Exception e) {
return "return--catch";
} finally {
return "finally";
}
}
执行结果:
finally
Process finished with exit code 0
总结
无return,无异常:try ->finally
无return,有异常:catch ->finally
try或catch中有return,无异常:try -> finally ->return(try)
try或catch中有return,有异常:try(未出现异常的前半段) -> catch ->finally->return(catch)
不论有没有异常,try或catch中有没有return:try/catch->return(finally)