201521123006 《java程序设计》 第9周学习总结

1. 本周学习总结

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

2. 书面作业

本次PTA作业题集异常

**1.常用异常 **

题目5-1

**1.1 截图你的提交结果(出现学号) **

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

以前经常出现的异常为数组越界,数组越界为RuntimeException,一般来说是由于编码错误,可以不捕获。我们可以尽量把数组的大小定义大些,或者根据输入定义数组大小,尽量改进代码。

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

ErrorRuntimeException都是unchecked exception代码中无需try-catch捕获处理外,其他异常都需要使用捕获处理。

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

题目5-2

**2.1 截图你的提交结果(出现学号) **

**2.2 实验总结 **

使用Integer.parseInt(in.next())对数组进行输入赋值,这时若输入的不是数字,则容易出现NumberFormatException 异常,这时需要我们对它进行捕获。并且因为是在for循环中使用,所以需要注意循环中是否把错误的输入也计算入了。

**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
    {
        /*
         * 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"); //String s 为null时,抛出异常NumberFormatException,输出“null”
        }

        if (radix < Character.MIN_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " less than Character.MIN_RADIX");//当radix < Character.MIN_RADIX时,抛出异常NumberFormatException及语句。
        }

        if (radix > Character.MAX_RADIX) {
            throw new NumberFormatException("radix " + radix +
                                            " greater than Character.MAX_RADIX");//当radix > Character.MIN_RADIX时,抛出异常NumberFormatException及语句。
        }

        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 != '+')//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;
    }

如在3.1中我们需要根据输入的不同抛出不同的异常,即IllegalArgumentException。抛出异常时需要传递给我们出现的异常为什么,以及输出我们在抛出异常时赋予他们的值或我们想要的输出格式,和我们出错的地方(导致异常的原因)。

**4.函数题 **

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

**4.1 截图你的提交结果(出现学号) **

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

对于可能抛出多个异常的try块,我们可以对每个异常都提供一个catch块进行处理,也可以不用。如果不对每个异常都提供catch块,此时就需要我们提供这些异常的父类,如Exception,使用catch块处理。当有多个catch块时,我们一定要让异常的范围按从小到大排列,因为如果子类异常在父类异常之后,那么子类异常的catch块将不会执行,父类异常的catch块就已经捕获了抛出的异常。当然如果是在相同继承层次的就可以不考虑排列顺序。

**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关闭资源。 **

byte[] content = null;
FileInputStream fis = null;
int bytesAvailabe = 0;
try {
        fis = new FileInputStream("testfis.txt");
        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{
		if(fis!=null){
		    try {
			    fis.close();
			} catch (IOException e) {
			    // TODO Auto-generated catch block
			    e.printStackTrace();
			}
	    }
    }

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

byte[] content = null;
int bytesAvailabe = 0;
try(FileInputStream fis= new FileInputStream("testfis.txt")) {
        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();
    }

**6.重点考核:使用异常改进你的购物车系统(未提交,得分不超过6分) **

举至少两个例子说明你是如何使用异常处理机制让你的程序变得更健壮。
说明要包含2个部分:1. 问题说明(哪里会碰到异常)。2.解决方案(关键代码)

输入不为数字

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        ShoppingDao shoppingDao = new ShoppingDaoJTableImpl(jTable1);
        String[] strs = jTextField1.getText().split(" ");
        try{
            String name = strs[0];
            Double price = Double.parseDouble(strs[1]);//当输入不为double型数字,抛出异常
            Integer num = Integer.parseInt(strs[2]);//当输入不为integer型数字,抛出异常
            Shopping s = new Shopping(name, price, num);
            boolean flag = shoppingDao .save(s);
            if(flag){
                JOptionPane.showMessageDialog(null, "添加成功");
            }else{
                JOptionPane.showMessageDialog(null, "添加失败!");
         }
        }catch(NumberFormatException e){
            out.setText("输入的商品价格和数量应为数字,请重输");
        }
}

找不到指定路径的文件

private void sumActionPerformed(java.awt.event.ActionEvent evt) {
    double sum=0.0;                                
    try(FileReader fis = new FileReader ("D:/testfis.txt");
        BufferedReader s=new BufferedReader(fis)) {//如果找不到文件,抛出异常
        Double price=0.0;
        Integer num=0;
        while(s.readLine()!=null){
        price=Double.parseDouble(in.readLine());
        num = Integer.parseInt(in.readLine());
        sum+=price*num;
    } catch (FileNotFoundException e) {
        out.setText("找不到文件");
    } catch (IOException e) {
	out.setText("文件读写异常");
    }
} 

**7.选做:JavaFX入门 **

如果未完成作业1、2的先完成1、2。贴图展示。如果已完成作业1、2的请完成作业3。内有代码,可在其上进行适当的改造。建议按照里面的教程,从头到尾自己搭建。

**8.选做:课外练习 **

JavaTutorial中Questions and Exercises
练习总结

3. 码云上代码提交记录

题目集:异常

**3.1. 码云代码提交记录 **

  • 在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

选做:4. 课外阅读

任选下面一篇文章阅读,列举出几点自己能理解的异常处理最佳实践。
Best Practices for Exception Handling
Exception-Handling Antipatterns Blog
The exceptions debate

posted @ 2017-04-22 02:45  HeeCHUL  阅读(235)  评论(0编辑  收藏  举报