packagecom.hspedu.customexception_;publicclassHomework01{publicstaticvoidmain(String[] args){try{//先验证输入的个数是否正确 两个参数if(args.length !=2){thrownewArrayIndexOutOfBoundsException("参数个数不对");}//先把接收到的参数 ,转成整数int n1 =Integer.parseInt(args[0]);int n2 =Integer.parseInt(args[1]);double res =cal(n1,n2);//该方法可能抛出ArithmeticExceptionSystem.out.println("计算结果是="+ res );}catch(ArrayIndexOutOfBoundsException e){thrownewRuntimeException(e);}catch(NumberFormatException e){System.out.println("参数格式不正确,需要输出整数");}catch(ArithmeticException e){System.out.println("出现了除0异常");}}publicstaticdoublecal(int n1,int n2){return n1/n2;}}
测试题3
packagecom.hspedu.customexception_;publicclassHomework02{publicstaticvoid main (String[] args){//args.length = 0;//这里发生的是 ArrayIndexOutOfBoundsExceptionif(args[4].equals("john")){System.out.println("AAA");}else{System.out.println("BBB");}Object o = args[2];//String->Object,向上转型Integer i =(Integer) o;//错误,这里一定会发生 ClasscastException}}