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
热门推荐
1 训练狗狗基础
2 怎么训练狗狗笑
3 怎么训练狗狗卧下
4 上海狗狗训练基地
5 训练狗狗排便
6 狗狗基础训练
8 如何训练狗狗装死
9 狗狗站姿训练
10 狗狗训练用品
11 训练狗狗吧
12 狗狗要怎么训练
13 怎么训练狗狗玩球
14 训练狗狗唤回
15 狗狗训练躺下
16 狗狗训练指令
17 狗狗的训练技巧
18 狗狗不好训练