📄 demonrethrow.java
字号:
//重新抛出异常示例。
class Rethrow {
public static void generateException() {
// longArray数组的长度大于shortArray
int longArray[] = {2, 4, 8,16, 32, 64,128};
int shortArray[] = { 1, 0, 2, 2, 0 ,2};
for(int i=0; i< longArray.length; i++) {
try {
System.out.println(longArray [i] + " / " +
shortArray [i] + " is " +
longArray [i]/ shortArray [i]);
}
catch (ArithmeticException e) {
// 捕捉异常
System.out.println("Can't divide by Zero!");
}
catch (ArrayIndexOutOfBoundsException e) {
// 捕捉异常
System.out.println("No matching element found.");
throw e; // 重新抛出异常
}
}
}
}
class DemonRethrow {
public static void main(String args[]) {
try {
Rethrow.generateException();
}
catch(ArrayIndexOutOfBoundsException e) {
//捕获重新抛出的异常
System.out.println("error!! -- " +
"program terminated.");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -