目录
457-异常课后作业
458-异常课后作业2
457-异常课后作业
package chapter12.exception.homework;
/**
* @author LuHan
* @version 1.0
*/
public class Homework01 {
public static void main(String[] args) {
try {
if(args.length!=2){
throw new ArrayIndexOutOfBoundsException("参数个数要是2个");
}
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;
}
}