zoukankan      html  css  js  c++  java
  • 第九周-异常

    1. 本周学习总结

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

    2. 书面作业

    本次PTA作业题集异常

    1.常用异常

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

    1.2 自己以前编写的代码中经常出现什么异常、需要捕获吗(为什么)?应如何避免?
    答: 自己编写的代码中经常会出现ArrayIndexOutOfBoundsException异常和NullPointerExceptin异常,这两个异常都不需要捕获。因为ArrayIndexOutOfBoundsException异常和NullPointerExceptin是同属于RuntimeException(Unchecked Exception),所以可不用捕获。
    1.3 什么样的异常要求用户一定要使用捕获处理?
    答:除了RuntimeException以外的异常都是Checked Exception的,这些异常都需要捕获。

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

    题目5-2
    2.1 截图你的提交结果(出现学号)

    2.2 实验总结
    答:本题需要注意的是,在捕获了不属于整型的字符后,在数组的该位置需要减一,保证不会出现数组在该位置跳一位,从而达到填满数组的目的。

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

    答:在抛出异常时需要告诉调用者哪里出了错误。就比如在5-3中,根据输入的begin和end所造成的IllegalArgumentException异常抛出以及begin和end不同的情况,可以提醒调用者出错的原因,例如begin>=end时,抛出异常后再加上“begin>=end”之类的信息,可以使调用者明确知道出错误的地方。

    4.函数题

    题目4-1(多种异常的捕获)
    4.1 截图你的提交结果(出现学号)

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

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

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

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

    1.输入数量时也许会出现不是数字的字符;
    用户名或密码输入错误;
    调用文件里的商品信息出错等。
    2.

    小代码:
    	Scanner in=new Scanner(System.in);
    	int num = 0;
    	try {
    	    num =in.nextInt();
    	} catch (Exception e) {
    	    System.out.println(e + "输入错误");
    	}
    	System.out.println(num);
        }
    }
    

    3. 码云上代码提交记录

    题目集:异常

    3.1. 码云代码提交记录

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

  • 相关阅读:
    IOS-多线程知识
    《shop》 --- 自定义工具类 分页功能
    navicat for mysql 显示中文乱码解决办法
    Linux -- 搭建php服务器环境小记
    在自己主机搭建svn服务器,在远程地址里搭建svn服务器
    $ThinkPhp学习,shop项目 小记
    php 个人博客 实战小记
    php:require 和 include 的区别 5.9日
    mysql的入门命令 ==留言本的思路==
    Access denied for user 'root'@'localhost' (using password: YES) 问题解决小记
  • 原文地址:https://www.cnblogs.com/chendajia/p/6746863.html
Copyright © 2011-2022 走看看