image.png

    1. package com.atguigu.exercise1;
    2. public class EcmDef {
    3. public static void main(String[] args) {
    4. try{
    5. int i = Integer.parseInt(args[0]);
    6. int j = Integer.parseInt(args[1]);
    7. int result = ecm(i,j);
    8. System.out.println(result);
    9. }catch(NumberFormatException e){
    10. System.out.println("数据类型不一致");
    11. }catch(ArrayIndexOutOfBoundsException e){
    12. System.out.println("缺少命令行参数");
    13. }catch(ArithmeticException e){
    14. System.out.println("算术异常");
    15. }catch(EcDef e){
    16. System.out.println(e.getMessage());
    17. }
    18. }
    19. public static int ecm(int i,int j) throws EcDef{
    20. if(i < 0 || j < 0){
    21. throw new EcDef("分子或分母为负数了!");
    22. }
    23. return i / j;
    24. }
    25. }
    1. package com.atguigu.exercise1;
    2. //自定义异常类:
    3. public class EcDef extends Exception{
    4. static final long serialVersionUID = -7034896193246939L;
    5. public EcDef(){
    6. }
    7. public EcDef(String msg){
    8. super(msg);
    9. }
    10. }