7.13rethrowexception.java
来自「JAVA程序设计的源代码」· Java 代码 · 共 27 行
JAVA
27 行
public class RethrowException{
private static int a=10,b=0;
public static void main(String args[]){
try{
divideOperation();
}
catch(ArithmeticException exception){ //捕获被零除的异常
System.out.println("ArithmeticException is handled in mainmethod!");
System.err.println(exception.toString());
}
}
public static void divideOperation () throws ArithmeticException{ //声明被零除的异常
try {
if (b == 0){
System.err.println(" --- Calling method divideoperation---");
throw new ArithmeticException ("Divided by zero!"); //抛出被零除的异常
}
System.out.println ("a / b =" + a / b);
}
catch (ArithmeticException exception){ //捕获被零除的异常
System.err.println("ArithmeticException is caught in divideOperation method!");
System.err.println("ArithmeticException is thrown in divideOperation method!");
throw exception;
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?