• 201521123080《Java程序设计》第9周学习总结


    1. 本周学习总结

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

    2. 书面作业

    本次PTA作业题集异常

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

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

    • 经常出现ArrayIndexOutOfBoundsException,不需要捕获,因为是在运行时产生的,要避免此类问题,要考虑数组的容量并在使用时注意不要越界。
      1.3 什么样的异常要求用户一定要使用捕获处理?
    • RuntimeException或者RuntimeException子类的异常不需要进行声明捕捉。
    1. 处理异常使你的程序更加健壮
      题目5-2
      2.1 截图你的提交结果(出现学号)

      2.2 实验总结
    • 本题首先实例化一个数组,然后循环输入数据,即for(int i=0;i<arr.length;)这边的i++需要放在可能会发生异常的输入语句下面,用try-catch来实现当输入的是非整型字符串提示异常。
    1. throw与throws
      题目5-3
      3.1 截图你的提交结果(出现学号)

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

    • 源代码如下:
    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;
        }
    
    • 运行程序后需要对这段代码抛出什么异常及原因反馈给程序员,如当数组越界是抛出ArrayIndexOutOfBoundsException。
    1. 函数题
      题目4-1(多种异常的捕获)
      4.1 截图你的提交结果(出现学号)

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

    • 处理多种异常时,Exception子类的异常处理要放在父类的异常处理之前。
    1. 为如下代码加上异常处理
        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关闭资源。
    
    • 改正如下:
    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 (Exception e) {
           System.out.println(e);
            } 
          finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (Exception e) {
                        System.out.println(e);
                    }
                }
            }
    
    5.2 使用Java7中的try-with-resources来改写上述代码实现自动关闭资源.
    
     try(FileInputStream fis= new FileInputStream("testfis.txt"))
            { 
              int bytesAvailabe = fis.available();
              if(bytesAvailabe>0){
              content = new byte[bytesAvailabe];
              fis.read(content);
                }
            }
            catch(FileNotFoundException e){
                System.out.println(e);
            }
            catch(Exception e){
                System.out.println(e);
            }
            System.out.println(Arrays.toString(content));
    

    3. 码云上代码提交记录

    题目集:异常
    3.1. 码云代码提交记录

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

  • 相关阅读:
    maven
    面试宝典之Java程序运行原理
    并发队列总结
    HashMap探究
    Linux安装python应用之前需要安装的库
    Linux中为Python应用安装uwsgi
    常用SQL语句
    Windows CE无法连接Win 10
    无法加载 DLL xpstar.dll 或它引用的一个 DLL。原因: 126(找不到指定的模块。)。
    SQL Server 数据恢复
  • 原文地址:https://www.cnblogs.com/zfy666/p/6748949.html
Copyright © 2020-2023  润新知