Java编程中的异常传播
异常传播是指从一个捕获块到另一个捕获块的异常流。以下是规则-
如果在受保护的代码中发生异常,则将该异常引发到列表中的第一个catch块。
如果抛出的异常的数据类型与ExceptionType1相匹配,它将在此处被捕获。
如果不是,则异常传递到第二个catch语句。
这一直持续到异常被捕获或陷入所有捕获为止。
在最终情况下,当前方法停止执行,并且异常被丢弃到调用堆栈中的前一个方法,此过程称为异常传播。
示例
public class Tester {
public static void main(String args[]) {
int a,b;
try {
a = Integer.parseInt(args[0]);
b = Integer.parseInt(args[1]);
int c = a/b;
System.out.println(c);
} catch(ArrayIndexOutOfBoundsException ex) {
System.out.println("Please pass the args while running the program");
} catch(NumberFormatException e) {
System.out.println("String cannot be converted as integer");
} catch(ArithmeticException e1) {
System.out.println("Division by zero is impossible"); }finally {
System.out.println("The program is terminated");
}
}
}输出结果
Please pass the args while running the program The program is terminated
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语