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

1.本章学习总结

2. 书面作业

本次PTA作业题集异常

1.常用异常

题目5-1

Q1.1 截图你的提交结果(出现学号)

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

常见的异常为数组越界以及空指针异常,对数组越界问题来说,我们应在编写代码时自行注意避免此问题,检测数组下标是否越界即可,无需使用try-catch处理。

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

Checked Exception一定要使用try-catch进行捕获处理(除了ErrorRuntimeException及其子类以外的异常都是Checked Exception)。

2.处理异常使你的程序更加健壮

题目5-2

Q2.1 截图你的提交结果(出现学号)

Q2.2 实验总结

本题在实验课上老师有给出大致参考代码,较为简单,目测只是让我们熟悉try-catch的应用。注意的地方在于发现异常后为了重新输入,应i--。

3.throw与throws

题目5-3

Q3.1 截图你的提交结果(出现学号)

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

Integer.parsetInt源代码如下:

public static int parseInt(String s) throws NumberFormatException {
        return parseInt(s,10);
    }
public static int parseInt(String s, int radix)
                throws NumberFormatException
    {
        /*
         * WARNING: This method may be invoked early during VM initialization
         * before IntegerCache is initialized. Care must be taken to not use
         * the valueOf method.
         */

        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;
    }

大体来说,主要传递给调用者异常的具体原因。可对不同的异常抛出不同的显示,显示出异常类及异常的原因。

4.函数题

题目4-1(多种异常的捕获)

Q4.1 截图你的提交结果(出现学号)

Q4.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));//打印数组内容

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

代码如下:

byte[] content = null;
FileInputStream fis = null ;
try {
    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));//打印数组内容
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}finally{
    try {
        fis.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block          
        e.printStackTrace();
    }
}

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

代码如下:

byte[] content = null;
try (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));// 打印数组内容
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

3. 码云上代码提交记录

posted @ 2017-04-22 13:26  漂流草子  阅读(161)  评论(1编辑  收藏  举报