catch
或者finally
中有return
语句时,catch
和finally
代码块之后的程序部分将不会被执行到。catch
和finally
中都存在return
语句,最终的返回值将是finally
中的return
语句所指定的值。try
代码块中是否出现异常,只要try
或catch
执行到了return
之前,finally
代码块都会被执行。try
代码块中使用return
语句。package org.example.a;
public class Demo {
public static void main(String[] args) {
Object handler = handler();
System.out.println(handler.toString());
}
public static Object handler() {
try {
System.out.println("try:内(前)");
System.out.println("try:内(异常)" + 5 / 0);
System.out.println("try:内(后)");
return "try:返回";
} catch (Exception e) {
System.out.println("catch:内(前)");
// System.out.println("catch:内(异常)" + 5 / 0);
// System.out.println("catch:内(后)");
// return "catch:返回";
} finally {
System.out.println("finally:内");
// return "finally:返回";
}
System.out.println("最后");
return "最后(返回)";
}
}
执行结果(try=> catch=> finally=> finally 块之外):
try:内(前)
catch:内(前)
finally:内
最后
最后(返回)
分析:try
中出现异常,跳转到catch
执行,catch
执行完后执行finally
,finally
执行完后继续执行finally
之后的代码。
package org.example.a;
public class Demo {
public static void main(String[] args) {
Object handler = handler();
System.out.println(handler.toString());
}
public static Object handler() {
try {
System.out.println("try:内(前)");
// System.out.println("try:内(异常)" + 5 / 0);
// System.out.println("try:内(后)");
return "try:返回";
} catch (Exception e) {
System.out.println("catch:内(前)");
System.out.println("catch:内(异常)" + 5 / 0);
System.out.println("catch:内(后)");
return "catch:返回";
} finally {
System.out.println("finally:内");
// return "finally:返回";
}
}
}
执行结果(try=> finally=> try 的 return):
try:内(前)
finally:内
try:返回
分析:try
中无异常,执行到try
中的return
之前先执行finally
,然后返回try
中的return
值。
package org.example.a;
public class Demo {
public static void main(String[] args) {
Object handler = handler();
System.out.println(handler.toString());
}
public static Object handler() {
try {
System.out.println("try:内(前)");
// System.out.println("try:内(异常)" + 5 / 0);
// System.out.println("try:内(后)");
return "try:返回";
} catch (Exception e) {
System.out.println("catch:内(前)");
System.out.println("catch:内(异常)" + 5 / 0);
System.out.println("catch:内(后)");
return "catch:返回";
} finally {
System.out.println("finally:内");
return "finally:返回";
}
}
}
执行结果(try=> finally=> 程序结束(不调用 try 的 return)):
try:内(前)
finally:内
finally:返回
分析:try
中无异常,执行到try
中的return
之前先执行finally
,finally
中有return
,直接返回finally
中的值,不再执行try
中的return
。
package org.example.a;
public class Demo {
public static void main(String[] args) {
Object handler = handler();
System.out.println(handler.toString());
}
public static Object handler() {
try {
System.out.println("try:内(前)");
System.out.println("try:内(异常)" + 5 / 0);
System.out.println("try:内(后)");
return "try:返回";
} catch (Exception e) {
System.out.println("catch:内(前)");
// System.out.println("catch:内(异常)" + 5 / 0);
// System.out.println("catch:内(后)");
return "catch:返回";
} finally {
System.out.println("finally:内");
// return "finally:返回";
}
}
}
执行结果(try=> catch=> finally=> catch 的 return):
try:内(前)
catch:内(前)
finally:内
catch:返回
分析:try
中出现异常,跳转到catch
执行,catch
中有return
,在执行catch
中的return
之前先执行finally
,然后返回catch
中的值。
package org.example.a;
public class Demo {
public static void main(String[] args) {
Object handler = handler();
System.out.println(handler.toString());
}
public static Object handler() {
try {
System.out.println("try:内(前)");
System.out.println("try:内(异常)" + 5 / 0);
System.out.println("try:内(后)");
return "try:返回";
} catch (Exception e) {
System.out.println("catch:内(前)");
// System.out.println("catch:内(异常)" + 5 / 0);
// System.out.println("catch:内(后)");
return "catch:返回";
} finally {
System.out.println("finally:内");
return "finally:返回";
}
}
}
执行结果(try=> catch=> finally=> 程序退出):
try:内(前)
catch:内(前)
finally:内
finally:返回
分析:try
中出现异常,跳转到catch
执行,catch
中有return
,在执行catch
中的return
之前先执行finally
,finally
中也有return
,最终返回finally
中的值。
package org.example.a;
public class Demo {
public static void main(String[] args) {
Object handler = handler();
System.out.println(handler.toString());
}
public static Object handler() {
try {
System.out.println("try:内(前)");
System.out.println("try:内(异常)" + 5 / 0);
System.out.println("try:内(后)");
return "try:返回";
} catch (Exception e) {
System.out.println("catch:内(前)");
System.out.println("catch:内(异常)" + 5 / 0);
System.out.println("catch:内(后)");
return "catch:返回";
} finally {
System.out.println("finally:内");
// return "finally:返回";
}
}
}
执行结果(try=> catch=> finally=> 程序结束(catch 不会再 return)):
Exception in thread "main" java.lang.ArithmeticException: / by zero
at org.example.a.Demo.handler(Demo.java:18)
at org.example.a.Demo.main(Demo.java:5)
try:内(前)
catch:内(前)
finally:内
分析:try
中出现异常,跳转到catch
执行,catch
中又出现异常,在执行catch
中的return
之前先执行finally
,由于catch
中的异常未被处理,导致程序终止。