Java笔记022-异常-Exception章节练习

异常-Exception章节练习

1、编程题

a、编写应用程序EcmDef.java,接收命令行的两个参数(整数),计算两数相除。

b、计算两个数相除,要求使用方法 cal(int n1, int n2)

c、对数据格式不正确(NumberFormatException)、

缺少命令行参数(ArrayIndexOutOfBoundsException)、

除0进行异常处理(ArithmeticException)

package com12.exercise;

/**
 * @author 甲柒
 * @version 1.0
 * @title Exercise01
 * @package com12.exercise
 * @time 2023/1/9 20:54
 */
public class Exercise01 {
    public static void main(String[] args) {
//a、编写应用程序EcmDef.java,接收命令行的两个参数(整数),计算两数相除。
//b、计算两个数相除,要求使用方法 cal(int n1, int n2)
//c、对数据格式不正确(NumberFormatException)、
//缺少命令行参数(ArrayIndexOutOfBoundsException)、
//除0进行异常处理(ArithmeticException)

        try {
            //先验证输入的参数的个数是否正确 两个参数
            if (args.length != 2) {
                throw new ArrayIndexOutOfBoundsException("参数个数不对");
            }

            //先把接收到的参数 转成整数
            int n1 = Integer.parseInt(args[0]);
            int n2 = Integer.parseInt(args[1]);

            double result = cal(n1, n2);//该方法可能抛出ArithmeticException
            System.out.println("计算结果=" + result);

        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println(e.getMessage());
        } 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;
    }
}

2、说出以下代码是否会发生异常,如果会,是哪种异常?如果不会,则打印结果是什么

package com12.exercise;

/**
 * @author 甲柒
 * @version 1.0
 * @title Exercise02
 * @package com12.exercise
 * @time 2023/1/9 21:10
 */
public class Exercise02 {
    public static void main(String[] args) {
        //args.length = 0
        //这里发生的是ArrayIndexOutOfBoundsException
        if (args[4].equals("john")) {//可能发生NullPointerException
            System.out.println("AA");
        } else {
            System.out.println("BB");
        }
        Object o = args[2];//String -> Object 向上转型
        Integer i = (Integer) o;//错误 这里一定会发生ClassCastException
    }
}

3、写出程序结果BCD

package com12.exercise;

/**
 * @author 甲柒
 * @version 1.0
 * @title Exercise03
 * @package com12.exercise
 * @time 2023/1/9 21:18
 */
public class Exercise03 {
    public static void func() {
        try {
            throw new RuntimeException();
        } finally {
            System.out.println("B");
        }
    }

    public static void main(String[] args) {
        try {
            func();
            System.out.println("A");
        } catch (Exception e) {
            System.out.println("C");
        }
        System.out.println("D");
    }
}

4、写出程序结果BCD

package com12.exercise;

/**
 * @author 甲柒
 * @version 1.0
 * @title Exercise04
 * @package com12.exercise
 * @time 2023/1/9 21:23
 */
public class Exercise04 {
    public static void main(String[] args) {
        try {
            showExce();
            System.out.println("A");
        } catch (Exception e) {
            System.out.println("B");//1
        } finally {
            System.out.println("C");//2
        }
        System.out.println("D");//3
    }

    public static void showExce() throws Exception {
        throw new Exception();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_59621600/article/details/128621011
今日推荐