1、如果catch或者finally中有return,则catch和finally代码块之后的部分根本不会执行到
2、如果catch和finally中都有return,后边的(也就是finally的)return会作为返回值
3、try,catch执行到了return之前都会执行finally
4、不要在try里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无异常, 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=> finally=> try的return)
try:内(前)
finally:内
try:返回
三、try无异常, 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:内