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

    201521123122 《java程序设计》第九周实验总结


    1. 本周学习总结

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


    2. 书面作业

    常用异常

    题目5-1

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

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

    1.以前的代码中,最经常出现的有ArrayIndexOutOfBoundsException(数组越界)、 NullPointerException(空指针)、ClassCastException(强制类型转换错误)等异常。
    2.这些异常is a RuntimeException,属于Unchecked Exception,不需要try-catch
    3.对代码进行改进

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

    在Exception子类中,除去RuntimeException及其子类,其他子类都是check Exception,也就是要求用户一定要使用捕获处理的异常,都需要try-catch来捕获。

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

    题目5-2

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

    2.2 实验总结

    参考ppt上的代码这题并不是很难,题目要求在输入非整形字符串时,要出现异常,所以对其输入进行捕获就可以,但要注意String inputInt = sc.next()String inputInt = sc.nextline()的区别。String inputInt = sc.next()在输入时不会得到带空格的字符串。如果使用String inputInt = sc.nextline()的话,编译结果如下图所示:

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

    抛出异常时,要出现此异常出现的原因,在Integer.parsetInt源代码中,利用if语句判断了在不同的条件下出现此异常,以及出现此异常的原因,在5-3实验中,也是如此,利用if判断begin>=end
    begin<0以及end>arr.length三种情况来说明出现异常的原因。

    函数题

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

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

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

    子类异常捕获必须在父类前面

    为如下代码加上异常处理

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

    public static void main(String[] args) throws IOException {
            byte[] content = null;
            try(FileInputStream 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);
            }
            System.out.println(Arrays.toString(content));//打印数组内容
    

    选做:JavaFX入门

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

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

    题目集:jmu-Java-05-集合

    3.1. 码云代码提交记录

  • 相关阅读:
    SAP MM 采购发票上的金额小差异
    SAP MM 物料号到物料的库存转移过账里的差异
    SAP MM 采购附加费在收货以及发票过账时候的会计分录
    SAP MM 移动平均价的商品发票价格和采购订单价格差异的处理
    WPF 使用 VisualBrush 在 4k 加 200 DPI 设备上某些文本不渲染看不见问题
    dotnet 写一个支持层层继承属性的对象
    dotnet OpenXML 读取 PPT 内嵌 xlsx 格式 Excel 表格的信息
    WPF 在 .NET Core 3.1.19 版本 触摸笔迹偏移问题
    linux下将编译错误输出到一个文本文件
    浮点型(FLOAT)与CHAR型转换
  • 原文地址:https://www.cnblogs.com/fenm/p/6748110.html
Copyright © 2011-2022 走看看