java-异常课后作业1
![请添加图片描述](https://img-blog.csdnimg.cn/0d3f500b1f5d49ada67d1c72055655bb.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBALea3seiTneOAgg==,size_20,color_FFFFFF,t_70,g_se,x 源码 package com.chapter433.Exception;
import jdk.internal.org.objectweb.asm.tree.MultiANewArrayInsnNode;
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);//该方法可能会抛出ArithmeticException
System.out.println("计算结果是=" + res);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}catch (NumberFormatException e){
System.out.println("参=参数格式不正确 需要输出整数");
}catch (ArithmeticException e){
System.out.println("出现了除0的异常");
}
}
//编写cal方法 就是两个数的商
public static double cal(int n1,int n2){
return n1/n2;
}
}
|