一句话:catch捕获后执行catch{}大括号内的代码,然后执行catch{}大括号相邻后面的代码
public class Main {
public static void main(String[] args) {
try {
test();
} catch (Exception e) {
;
}
System.out.println("执行");
}
public static void test()throws Exception{
try {
System.out.println("执行");
throw new Exception();
System.out.println("不执行");
} catch (Exception e) {
// 这里是 catch 块
System.out.println("执行");
throw new Exception();
System.out.println("不执行");
}
System.out.println("不执行");
}
}
throws
对于非受检异常,通常不需要在方法签名中使用 throws 关键字进行声明,因为编译器不会强制要求调用者进行处理。非受检异常在传播时,会沿着调用堆栈一直向上抛出,直到遇到处理代码或者到达程序的顶层。
非受检异常和受检异常:相对编译器检查
受检异常要么显式声明throws,要么trycatch,不然编译器会报错。
非受检异常默认可以抛出给调用者,可以一直传递到main。
本文详细解释了Java中的异常处理流程,包括try方法块中异常的处理、非try-catch块后的代码执行情况,以及受检异常和非受检异常的区别,强调了编译器对异常处理的要求。

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



