1. 本周学习总结
1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容。
2. 书面作业
本次PTA作业题集异常
1.常用异常
题目5-1
1.1 截图你的提交结果(出现学号)
1.2 自己以前编写的代码中经常出现什么异常、需要捕获吗(为什么)?应如何避免?
A:最经常出现的就是NullPointerExceptin了。因为其只有在程序运行时会出错,这属于Unchecked Exception,所以可不用捕获。在写程序的过程中多考虑对象为null的情况,用if语句可避免此类问题。
1.3 什么样的异常要求用户一定要使用捕获处理?
A:除了RuntimeException以外的异常要求用户一定要用捕获处理,即Checked Exception。
2.处理异常使你的程序更加健壮
题目5-2
2.1 截图你的提交结果(出现学号)
2.2 实验总结
A:这题需要考虑输入非数字之后数组下标位置的问题。
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为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) 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;}
A:最终抛出的都是NumberFormatException异常。不过提供给调用者的信息是根据情况而定的。 传入null的时候throw new NumberFormatException("null");给定的转化进制数超过规定的范围则throw new NumberFormatException("radix " + radix +" greater than Character.MAX_RADIX");当然,还有判断是否只输入了"+"、"-",以及转化后的数字是否超过了规定值。这些信息能够让调用者一目了然,自己到底是因为什么原因导致了异常。 至于 parseInt函数一开头就写一大堆判断抛出异常,我认为这是有必要的,在做转化之前肯定要判断给定的值是否符合条件。
4.函数题
题目4-1(多种异常的捕获)
4.1 截图你的提交结果(出现学号)
4.2 一个try块中如果可能抛出多种异常,捕获时需要注意些什么?
A:需要注意Exception的子类异常不能在Exception后被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关闭资源。
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);//将文件内容读入数组 } System.out.println(Arrays.toString(content));//打印数组内容 } catch (IOException e) { e.printStackTrace(); } finally{ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } }}
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);//将文件内容读入数组 } System.out.println(Arrays.toString(content));//打印数组内容 } catch (IOException e) { e.printStackTrace(); }}
6.重点考核:使用异常改进你的购物车系统(未提交,得分不超过6分)
举至少两个例子说明你是如何使用异常处理机制让你的程序变得更健壮。 说明要包含2个部分:1. 问题说明(哪里会碰到异常)。2.解决方案(关键代码)
public static Object readObjectFromFile(){ Map temp=null; File file =new File("users.dat"); FileInputStream in; try { in = new FileInputStream(file); ObjectInputStream objIn=new ObjectInputStream(in); temp= (Map) objIn.readObject(); objIn.close(); //System.out.println("read object success!"); } catch (IOException e) { System.out.println(e); } catch (ClassNotFoundException e) { System.out.println(e); } return temp;}
if( !user .equals("") && !password .equals("")){ users = (Map) ReadAndWriteUsers.readObjectFromFile(); users.put(user, password); ReadAndWriteUsers.writeObjectToFile(users); JOptionPane.showMessageDialog(null, "注册成功!!!"); } else JOptionPane.showMessageDialog(null, "用户名或密码为空 ", "错误 ", JOptionPane.ERROR_MESSAGE);}
A:注册用户时,每次会先读取文件中的用户信息,返回一个Map,再把新的用户push到Map,再写入文件。 之前遇到的情况就是,当文件内没有储存任何用户的时候,第一次注册用户就会出现空指针异常,使程序奔溃。
原因分析:若文件内没有存储数据。程序运行到ObjectInputStream objIn=new ObjectInputStream(in)这一步时,就会捕获到IOException异常,从而导致返回的temp(Map)为空,而紧接着又使用user.put(),自然就出现了空指针异常。
java.io.EOFExceptionException in thread "AWT-EventQueue-0" java.lang.NullPointerException
--
//解决方法catch (IOException e) { System.out.println(e); temp = new HashMap(); }
在catch加入 temp = new HashMap<String,String>(); 或者在声明的时候Map temp = new HashMap<String,String>(); 都可以解决这个问题,显示异常信息,依然可以成功注册。
try{ GoodTextArea1.setText(GetGoods.getGoods()[0].toString()); GoodTextArea2.setText(GetGoods.getGoods()[1].toString()); GoodTextArea3.setText(GetGoods.getGoods()[2].toString()); GoodTextArea4.setText(GetGoods.getGoods()[3].toString()); GoodTextArea5.setText(GetGoods.getGoods()[4].toString()); } catch(Exception e){ System.out.println("未导入商品"); }
还有就是在显示商品的时候也需要注意是不是成功导出商品了。