zoukankan      html  css  js  c++  java
  • 201521123017 《Java程序设计》第9周学习总结

    1. 本周学习总结

    2. 书面作业

    Q1.常用异常
    题目5-1
    1.1 截图你的提交结果(出现学号)
    1.2 自己以前编写的代码中经常出现什么异常、需要捕获吗(为什么)?应如何避免?
    1.3 什么样的异常要求用户一定要使用捕获处理?

    1.1 截图

    1.2 经常会出现ArrayIndexOutOfBoundsException- 下标越界异常,例如定义数组int[] a=new int[5],int[5]=0;但不需要捕获,ArrayIndexOutOfBoundsException是属于RuntimeException,只需要注意下标小于数组的长度,或是在代码中if来判断下标,当下标<数组,则不再对数组进行操作,例如,for(int i=0;;i++)中,对数组进行赋值,用if(n<size)来判断数组是否下标小于size,是否退出循环

    1.3 在Exception子类中,除去RuntimeException,其他子类都是check Exception,也就是要求用户一定要使用捕获处理的异常,都需要try-catch来捕获,如果不处理这些异常,则不能进行编译,例如:CloneNotSupportedException, DataFormatException, DatatypeConfigurationException, DestroyFailedException等异常

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

    2.1 截图

    2.2 实验总结:本题主要是非整型字符串的捕获,即NumberFormatException的捕获,解决的问题是在处理捕获的异常,输出异常,还需要将下标-1,在输入非整型字符串时,此下标的数组将因非整型字符串而被空置,此时需要将下标-1,不然该下标数组为0,将不合题意,例如本题定义长度为5的int数组,在第3个数组(a[2])输入值时,输入'a',此时捕获异常,但若是for循环中的i不进行操作,则下个值输入时,输入的值是赋给第4个数组(a[3]),而a[2]却跳过没赋值,a[2]为初始值0,所以需要在捕获异常时加入i的操作,i--

    Q3.throw与throws
    题目5-3
    3.1 截图你的提交结果(出现学号)
    3.2 阅读Integer.parsetInt源代码,结合3.1说说抛出异常时需要传递给调用者一些什么信息?

    3.1 截图

    3.2

    源代码:

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

    抛出异常时,应提醒调用者所输入的字符串是否是整型字符串,注释中characters in the string must all be decimal digits也表示字符串中的字符必须是十进制数字,除非第一个字符可能是一个ASCII减号符号“{”}({“代码” u005cu002d ' })来表示负数或ASCII加号“+”} { @代码({“代码” u005cu002b ' })来表示正数值(注释:the first character may be an ASCII minus sign {@code '-'}({@code 'u005Cu002D'}) to indicate a negative value or anASCII plus sign {@code '+'} ({@code 'u005Cu002B'}) to indicate a positive value.)在5-3中,根据输入的begin和end所造成的IllegalArgumentException异常抛出,根据begin和end不同的情况,提醒调用者出错的不同的信息,例如begin>=end时,抛出异常,throw new IllegalArgumentException("begin:"+begin+" >= "+"end:"+end);这样调用这也可在被告知出错的同时,也知道出错的理由是因为begin>=end,这样调用者就懂得如何去改正错误

    Q4.函数题
    题目4-1(多种异常的捕获)
    4.1 截图你的提交结果(出现学号)
    4.2 一个try块中如果可能抛出多种异常,捕获时需要注意些什么?

    4.1 截图

    4.2 需要注意子类异常必须放在父类异常前面,如果父类异常放在前面,则会执行父类异常的捕获而不执行子类异常的捕获,导致编译错误。
    例如

    子类异常在父类异常前
    try{
       ...
    }catch(NumberFormatException e){//Exception的子类异常NumberFormatException
        System.out.println(e);//执行,输出异常NumberFormatException
    }catch(ArrayIndexOutOfBoundsException e){//Exception的子类异常ArrayIndexOutOfBoundsException
        System.out.println(e);//执行,输出异常ArrayIndexOutOfBoundsException
    }catch(Exception e){
        System.out.println(e);//执行,输出Exception异常的其它异常子类
    }
    
    子类异常在父类异常后
    try{
       ...
    }catch(Exception e){
        System.out.println(e);//执行,输出Exception异常的其它异常子类
    }catch(ArrayIndexOutOfBoundsException e){
        System.out.println(e);//不会执行
    }catch(NumberFormatException e){
        System.out.println(e);//不会执行
    }
    编译报错
    

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

    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来改写上述代码实现自动关闭资源.

    • 5.1 在不知道可能出现的异常的情况下,可以使用catch(Exception e)来捕获异常,基本所有的的异常类似NumberFormatException,NullPointerException等都是Exception的子类异常,可以通过Exception父类异常检测并输出异常;fis的关闭,不能直接关闭fis,需要捕获fis是否是空指针异常,才能关闭,否则运行时程序崩溃
    public static void main(String[] args) throws IOException{
        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);//将文件内容读入数组
    	}
    	}catch(Exception e){
                System.out.println(e);
    	}finally{
    	    if(fis!=null){
                    try{
                        fis.close();
    		}catch(NullPointerException e){//判断是否是空指针异常
                        System.out.println(e);
    		}
                }
    	}
    	System.out.println(Arrays.toString(content));//打印数组内容
    }
    
    • 5.2 Java7中提供了自动尝试关闭资源的语法,可将尝试自动关闭资源的对象生成写在try之后的圆括号中,无需判断是否为null,也避免了在关闭时产生的其它异常
    public static void main(String[] args) throws IOException{
        byte[] content = null;
        try(FileInputStream fis = new FileInputStream("testfis.txt")){//在try的圆括号中写入将要关闭资源的对象
        int bytesAvailabe = fis.available();//获得该文件可用的字节数
        if(bytesAvailabe>0){
            content = new byte[bytesAvailabe];//创建可容纳文件大小的数组
            fis.read(content);//将文件内容读入数组
        }
        }catch(Exception e){
            System.out.println(e);
        }
        System.out.println(Arrays.toString(content));//打印数组内容
    }
    

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

    • 关键代码(问题说明(哪里会碰到异常)在代码注释中体现)
    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
        try{
        ShopDao shopDao = new ShopDaoJTableImpl(jTable1);
        String[] strs = jTextField1.getText().split(" ");//可能会是空的对象添加,NullPointerException异常
        String name = strs[0];
        double price=Double.parseDouble(strs[1]);//可能输入的不是Double型的字符串,转换时发生异常,NumberFormatException异常
        Integer num =Integer.parseInt(strs[2]);//可能输入的不是Integer型的字符串,转换时发生异常,NumberFormatException异常
        Shop shop = new Shop(name,price,num);
        boolean flag = shopDao.save(shop);
        if(flag){
            JOptionPane.show(null, "添加成功");
        }else{
            JOptionPane.show(null, "添加失败!");
        }
        }catch(NumberFormatException e){
            System.out.println(e);
            System.out.println("输入的参数类型不符合 String Double Integer");
        }catch(NullPointerException e){
            System.out.println(e);
             System.out.println("输入的参数为空");
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println(e);
             System.out.println("输入的参数个数未符合3个");
        }catch(Exception e){
            System.out.println(e);
             System.out.println("其它的错误");
        }
        }                             
    }
    
    • 效果截图

    3. 码云上代码提交记录及PTA实验总结

    3.1. 码云代码提交记录

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

  • 相关阅读:
    SQL 通配符
    正则表达式
    与运算(&)、或运算(|)、异或运算(^)、右移运算符(>>>)本质介绍
    博客园博客目录自动生成(页面目录)
    Linux查看并杀死被占用的端口
    Eclipse的环境配置
    L-Rui
    Web页面弹出窗口代码大全
    linux-用户
    linux-网络
  • 原文地址:https://www.cnblogs.com/gemola/p/6740684.html
Copyright © 2011-2022 走看看