try catch finally运行顺序简介说明
下文笔者讲述try,catch,finally的运行顺序简介说明,如下所示
在日常开发中,我们经常使用try,catch,finally关键字编写代码,那么有return
在日常开发中,我们经常使用try,catch,finally关键字编写代码,那么
在try,catch,finally代码中 try 代码块中有return finally中的代码也会运行 并且finally的代码在return代码块之前例:
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中有异常时
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)
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


