Java中的异常传播
从堆栈顶部引发异常时,Java中会发生异常传播。如果未捕获到该异常,则该异常会丢弃前面方法的调用堆栈。如果没有被发现,它将进一步下降到以前的方法。继续进行直到该方法到达调用堆栈的底部或陷入两者之间的某个位置为止。
示例
让我们看一个示例,该示例说明Java中的异常传播-
public class Example {
void method1() // generates an exception {
int arr[] = {10,20,30};
System.out.println(arr[7]);
}
void method2() // doesn't catch the exception {
method1();
}
// method1 drops down the call stack
void method3() // method3 catches the exception {
try {
method2();
} catch(ArrayIndexOutOfBoundsException ae) {
System.out.println("Exception is caught");
}
}
public static void main(String args[]) {
Example obj = new Example();
obj.method3();
}
}输出结果
输出如下-
Exception is caught
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短