windy_yong
2015-01-10 23:17
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-catch是捕获异常的语句块,而在try中直接扔出一个异常之后,会直接进入catch语句块中
Java入门第三季
409775 学习 · 4546 问题
相似问题