📄 throwmultiexceptiondemo.java
字号:
// 例 3.2.3 ThrowMultiExceptionDemo.java
import java.io.*;
class ThrowMultiException
{
public void test(int x,int y)
throws IOException,ArithmeticException,IllegalArgumentException
{
if(y == 0)
throw new ArithmeticException("发生了被零除的异常");
if(y < 0)
throw new IOException("发生了I/O方面的异常");
if(x == 0)
throw new IllegalArgumentException("调用了方法中非法的参数");
System.out.println(x/y);
}
}
class ThrowMultiExceptionDemo
{
public static void main(String[] args)
{
ThrowMultiException me = new ThrowMultiException();
System.out.println("The program begin to run");
try
{
me.test(5,0); // 除数等于零的情况
// me.test(5,-1); // 除数小于零的情况
// me.test(0,6); // 被除数为0的情况
// me.test(6,5); // 正常的情况
}catch(ArithmeticException ae){
System.out.println("Catch ArithmeticException:"+
ae.getMessage());
}catch(IOException ie){
System.out.println("Catch IOException:"+ie.getMessage());
}catch(Exception e){
System.out.println("Catch Exception:"+e.getMessage());
}finally{
System.out.println("进行清理工作");
}
System.out.println("The program is over");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -