目录
一、一览表
意义 | 位置 | 后面跟的东西 | |
throws | 异常处理的一种方式 | 方法声明处 | 异常类型 |
throw | 手动生成异常对象的关键字 | 方法体中 | 异常对象 |
二、异常课后作业
1.编写运用程序EcmDef.java接受命令行的两个参数,计算两数相除
2.计算两个数相除,要求使用方法cal(int n1,int n2)
3.对数据格式不正确(numberformatException),缺少命令行参数(ArrayIndexoutofException),除0进行异常处理
package com.chapter.customException_;
/**
* @version 1.0
* @auter liyang
*/
public class Homework01 {
public static void main(String[] args) {
try {
if(args.length!=2){
throw new ArrayIndexOutOfBoundsException("参数个数不对");
}
int n1=Integer.parseInt(args[0]);
int n2=Integer.parseInt(args[1]);
double res=cal(n1,n2);
System.out.println("计算结果:"+res);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
}catch (NumberFormatException e){
System.out.println("参数格式不正确,输入整数");
}catch (ArithmeticException e){
System.out.println("出现除0的异常");
}
}
public static double cal(int n1,int n2){
return n1/n2;
}
}