try中含有throw到底应该如何理解

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class excTest {
public static void main(String[] args) {
try {
System.out.println("try中含有throw");
throw new e1();
} catch (e1 e) {
System.out.println("fsasd");
System.out.println(e.getMessage());
}
}
}
public class e1 extends Exception {
public e1() {
super("dafsf");
}
}

运行后控制台显示:
try中含有throw
fsasd
dafsf

这个throw应该怎么理解?
不是把异常抛到main方法吗?按这里的逻辑应该是在catch块中输出啊!

——解决思路———————-
你把try catch去掉就抛到上层去了
——解决思路———————-
try 块中throw的异常,由异常处理链中最内层的能够处理该异常的catch捕获。

本文为网友分享,不保证正确!若侵权请联系我们删除!

Jerky Lu wechat
欢迎加入微信公众号