201521123066 《Java程序设计》第九周学习总结

1.本周学习总结

1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容。

2. 书面作业

1.常用异常
题目5-1
1.1 截图你的提交结果(出现学号)

1.2 自己以前编写的代码中经常出现什么异常、需要捕获吗(为什么)?应如何避免?

经常出现空指针,因为属于unchecked,所以不需要捕获,应当使用if语句进行判断
应使用类似于这样的判断语句:if (str != null) 调用str.length()

1.3 什么样的异常要求用户一定要使用捕获处理?

需要从错误中恢复的情况
除了Error与RuntimeException及其子类以外的异常都是Checked Exception
代码中必须try-catch进行捕获处理

2.处理异常使你的程序更加健壮
题目5-2
2.1 截图你的提交结果(出现学号)

2.2 实验总结

![](http://images2015.cnblogs.com/blog/1109952/201704/1109952-20170422125149134-1807427899.png)
如图为parseInt的源代码,会产生NumberFormatException异常,应该捕获;

3.throw与throws
题目5-3
3.1 截图你的提交结果(出现学号)

3.2 阅读Integer.parsetInt源代码,结合3.1说说抛出异常时需要传递给调用者一些什么信息?

 public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }


 public static int parseInt(String s, int radix)
                throws NumberFormatException
    {

        if (s == null) {
            throw new NumberFormatException("null");
        }

        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " less than Character.MIN_RADIX");
        }

        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " greater than Character.MAX_RADIX");
        }

        int result = 0;
        boolean negative = false;
        int i = 0, len = s.length();
        int limit = -Integer.MAX_VALUE;
        int multmin;
        int digit;

        if (len > 0) {
            char firstChar = s.charAt(0);
            if (firstChar < '0') { // Possible leading "+" or "-"
                if (firstChar == '-') {
                    negative = true;
                    limit = Integer.MIN_VALUE;
                } else if (firstChar != '+')
                    throw NumberFormatException.forInputString(s);

                if (len == 1) // Cannot have lone "+" or "-"
                    throw NumberFormatException.forInputString(s);
                i++;
            }
            multmin = limit / radix;
            while (i < len) {
                // Accumulating negatively avoids surprises near MAX_VALUE
                digit = Character.digit(s.charAt(i++),radix);
                if (digit < 0) {
                    throw NumberFormatException.forInputString(s);
                }
                if (result < multmin) {
                    throw NumberFormatException.forInputString(s);
                }
                result *= radix;
                if (result < limit + digit) {
                    throw NumberFormatException.forInputString(s);
                }
                result -= digit;
            }
        } else {
            throw NumberFormatException.forInputString(s);
        }
        return negative ? result : -result;
    }
如上为pasetInt的源代码,阅读源代码,在抛出异常的时候应该具体情况具体分析,用if语句指出不同情况,然后抛出对应的异常,
比如3.1的题目要求begin>0,当输入小于0的时候,抛出异常,并提示begin应该小于0。

4.函数题
题目4-1(多种异常的捕获)
4.1 截图你的提交结果(出现学号)

4.2 一个try块中如果可能抛出多种异常,捕获时需要注意些什么?

应该注意子类异常必须放在父类异常前面

5.为如下代码加上异常处理

    byte[] content = null;
    FileInputStream fis = new FileInputStream("testfis.txt");
    int bytesAvailabe = fis.available();//获得该文件可用的字节数
    if(bytesAvailabe>0){
        content = new byte[bytesAvailabe];//创建可容纳文件大小的数组
        fis.read(content);//将文件内容读入数组
    }
    System.out.println(Arrays.toString(content));//打印数组内容

5.1 改正代码,让其可正常运行。注1:里面有多个方法均可能抛出异常。注2:要使用finally关闭资源。

5.2 使用Java7中的try-with-resources来改写上述代码实现自动关闭资源.

6.重点考核:使用异常改进你的购物车系统(未提交,得分不超过6分)
举至少两个例子说明你是如何使用异常处理机制让你的程序变得更健壮。
说明要包含2个部分:1. 问题说明(哪里会碰到异常)。2.解决方案(关键代码)


(1)问题说明:当浏览到喜欢的商品想添加入购物车时,如果该商品售完,应该捕获异常,并说明商品已售完
     解决方案:ArrayList<Product> prolist = new ArrayList<Product>();
    public void addpro(Product pro){
        try{
        prolist.add(pro);
        }catch(NullPointerException e){
            System.out.println("None of the Product?"); 
        }
    }
(2)问题说明:输入选择添加商品的序号时当输入为字符串时,需要抛出输入格需要对输入的格式进行异常的捕获
     解决方案:
            System.out.println(Arrays.toString(p));
            System.out.println("选择添加商品序号:");
            try{
            switch (sc.nextInt()) {
            case 1: {
                cart.addpro(p[0]);

            }
            case 2: {
                cart.addpro(p[1]);
                break;
            }
            case 3: {
                cart.addpro(p[2]);
                break;
            }
            }
            menu(cart, p);
            }catch(NumberFormatException e){
                menu(cart,p);
            }

3. 码云上代码提交记录

3.1. 码云代码提交记录

原文地址:https://www.cnblogs.com/zxl3066/p/6747280.html