Java中捕获异常时,是否return对程序流程的影响。
文章目录
一、正常用法(try异常, catch/finally无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无异常, try有return, finally无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:内
try:返回
三 try无异常, try有return,finally有return(Idea报警告)
示例:
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有异常, catch有return, finally无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有return, finally有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=> 程序退出)
try:内(前)
catch:内(前)
finally:内
finally:返回
六 try有异常, catch有异常, finally无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))
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逻辑,finally是必须执行的
- 若try里面无异常,此时不进入catch,当try里面有return时,先于return执行finally中语句,当此时finall中无rerurn时,执行完finally语句再执行try中的return,当此时finally中有return时,则return执行finally中的,不执行try中的return,程序结束。
- 若try里面有异常,则进入catch,执行catch中语句,看catch里面是否有return,遇到return先执行finally,若finally中也有return则跳过catch中的return,需要注意:若执行的catch中也有异常,则执行到异常处直接执行finally,catch中处于异常后的语句都不执行(包含return)。