public class EcTest {
public static void main(String[] args) {
System.out.println(testReturn());
}
public static String testReturn() {
try {
int i = 5 / 0;
} catch (Exception e) {
// TODO: handle exception
return e.getMessage();
}
return "hhe";
}
public static void main(String[] args) {
System.out.println(testReturn());
}
public static String testReturn() {
try {
int i = 5 / 0;
} catch (Exception e) {
// TODO: handle exception
return e.getMessage();
}
return "hhe";
}
}
这里返回的是e.getMessage();的内容,如果catch里面有return 就返回
public static String testReturn() {
try {
int i = 5 / 0;
} catch (Exception e) {
e.printStackTrace();
}
return "hhe";
}
如果是这样,即打印堆栈信息又返回‘hhe’;
意思就是如果 catch中没有return 的话 执行完catch之后 会继续往下走。如果catch里面有return 就直接返回。
本文探讨了Java中异常处理的方法,并通过两个示例代码详细解释了如何在catch块中使用return语句来处理异常情况,同时展示了不同情况下程序的行为表现。

被折叠的 条评论
为什么被折叠?



