异常 Exception 练习题 (未完成)

try-catch异常处理

1

class Exception01 {
    public static int method() {
        try {
            String[] names = new String[3];//String[]数组
            if (names[1].equals("tom")) {//NullPointerException
                System.out.println(names[1]);
            } else {
                names[3] = "hspedu";
            }
            return 1;
        } catch (ArrayIndexOutOfBoundsException e) {
            return 2;
        } catch (NullPointerException e) {//捕获
            return 3;
        } finally { //必须执行
            return 4; //返回4
        }
    }

    public static void main(String[] args) {
        System.out.println(method()); //4
    }
}

String[] names = new String[3];的意思是开辟了一个数组空间,每个元素都是一个String类,默认指向null。第五行if (names[1].equals(“tom”)) 的时候,name[1]是空指针,name[1].equals一定抛出异常,就会是NullPointerException,所以是return 3,但是这里有finally,还是会return 4.

2

public class TryCatchExercise02 {
}

class Exception02 {
    public static int method() {
        int i = 1;
        try {
            i++; //i = 2
            String[] names = new String[3];
            if (names[1].equals("tom")) {//空指针
                System.out.println(names[1]);
            } else {
                names[3] = "hspedu";
            }
            return 1;
        } catch (ArrayIndexOutOfBoundsException e) {
            return 2;
        } catch (NullPointerException e) {
            return ++i; //i = 3
        } finally {//必须执行
            return ++i; //i = 4
        }
    }

    public static void main(String[] args) {
        System.out.println(method());
    }
}

OUT: 4

3

class ExceptionExe01 {
    public static int method() {
        int i = 1;//i = 1
        try {
            i++;// i=2
            String[] names = new String[3];
            if (names[1].equals("tom")) { //空指针
                System.out.println(names[1]);
            } else {
                names[3] = "hspedu";
            }
            return 1;
        } catch (ArrayIndexOutOfBoundsException e) {
            return 2;
        } catch (NullPointerException e) {
            return ++i;  // i = 3 => 保存临时变量 temp = 3;
        } finally {
            ++i; //i = 4
            System.out.println("i=" + i);// i = 4
        }
    }

    public static void main(String[] args) {
        System.out.println(method());// 3
    }
}

NullPointerException 异常,catch中 i = 3; 但是先不return, 注意底层会保存在一个临时变量中 temp=3;
然后执行 finally,i=4,输出 i = 4。
然后回到NullPointerException 的catch中,还是会返回临时变量。
输出:
i=4
3

4

题目:如果用户输入的不是一个整数,就提示他反复输入,直到输入一个整数为止。

不会写!!!!ohhhhhhhhnononono/(ㄒoㄒ)/

public class TryCatchExercise04 {
    public static void main(String[] args) {

        //如果用户输入的不是一个整数,就提示他反复输入,直到输入一个整数为止
        //思路
        //1. 创建Scanner对象
        //2. 使用无限循环,去接收一个输入
        //3. 然后将该输入的值,转成一个int
        //4. 如果在转换时,抛出异常,说明输入的内容不是一个可以转成int的内容
        //5. 如果没有抛出异常,则break 该循环
        Scanner scanner = new Scanner(System.in);
        int num = 0;
        String inputStr = "";
        while (true) {

            System.out.println("请输入一个整数:"); //
            inputStr = scanner.next();
            try {
                num = Integer.parseInt(inputStr); //这里是可能抛出异常
                break;
            } catch (NumberFormatException e) {
                System.out.println("你输入的不是一个整数:");
            }
        }

        System.out.println("你输入的值是=" + num);
    }
}

异常

1(没有自己写)

a)编写应用程序EcmDef.java,接收命令行的两个参数(整数),计算两数相除。
b)计算两个数相除,要求使用方法cal(int n1, int n2)。
c)对数据格式不正确、缺少命令行参数、除0进行异常处理。

public class Homework01 {
    public static void main(String[] args) {
        /*
        编写应用程序EcmDef.java,接收命令行的两个参数(整数),计算两数相除。
计算两个数相除,要求使用方法 cal(int n1, int n2)
对数据格式不正确(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 res = cal(n1, n2);//该方法可能抛出ArithmeticException
            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的异常");
        }


    }
    //编写cal方法,就是两个数的商
    public static double cal(int n1, int n2) {
        return n1 / n2;
    }
}

2

判断是否会发生异常?

public class Homework02 {
    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

    }
}

空指针


可能发生NullPointerException
可能发生 ClassCastException

3

判断正误

public class Homework03 {
    public static void func() {//静态方法
        try {
            throw new RuntimeException();
        } finally {
            System.out.println("B");
        }
    }

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


}

B
A
C
D

注意: 不会输出A,因为在try代码块中 抛出异常之后是不会执行接下来的代码的。 正确答案是:B C D

4

判断正误

    public static void main(String[] args) {//main方法
        try {
            showExce();
            System.out.println("A");
        } catch (Exception e) {
            System.out.println("B");
        } finally {
            System.out.println("C");
        }
        System.out.println("D");
    }

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

}

B
C
D

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值