201521123013 《Java程序设计》第9周学习总结
1. 本章学习总结
2. 书面作业
Q1.常用异常题目5-1
**1.1 截图你的提交结果(出现学号) **
1.2 自己以前编写的代码中经常出现什么异常、需要捕获吗(为什么)?应如何避免?
- 经常出现ClassCastException、EmptyStackException、IllegalArgumentException、ArrayIndexOutOfBoundsException、 NullPointerException。
- 这些异常都是is a RuntimeException,可以不用捕获。因为这些异常是虚拟机在运行时产生的异常,其产生比较频繁,如果都要处理对程序可读性和运行效率影响太大。RuntimeException在Java设计中把所有方法都默认定义在throws中了,所以你可以不用处理,系统会自动抛出。当然,必要时用户也可对其处理。
- 对于RuntimeException,一般来说是由于编码失误,尽量改进代码避免出现此类错误。
1.3 什么样的异常要求用户一定要使用捕获处理?
- Error和RuntimeException外的Exception的其他子类都要捕获处理。
Q2.处理异常使你的程序更加健壮题目5-2
2.1 截图你的提交结果(出现学号)
2.2 实验总结
- 捕获异常时,需要执行
i--;
,否则会出现如下图错误:
Q3.throw与throws题目5-3
3.1 截图你的提交结果(出现学号)
3.2 阅读Integer.parsetInt源代码,结合3.1说说抛出异常时需要传递给调用者一些什么信息)
public static int parseInt(String s, int radix)
throws NumberFormatException
{
//第一个参数是null,抛出异常并指明是字符串null
if (s == null) {
throw new NumberFormatException("null");
}
//基数小于2进制或者大于36进制,抛出异常为基数超过MIN_RADIX,MAX_RADIX
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");
}
//字符串长度超过1,除第一个字符是'-',串还存在不是由指定基数的数字表示的字符。抛出异常信息为输入字符串为不能解析的字符串
if (len > 0) {
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
} 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) {
digit = Character.digit(s.charAt(i++),radix);
...
throw NumberFormatException.forInputString(s);
...
}
} else { //字符串表示的值不是int类型
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
- 从Integer.parseInt()源代码中,可以看出根据不同的情况都会抛出异常,可以根据不同情况抛出具体的异常信息,以便调用者了解异常原因。在pta5-3中,对[begin,end]范围内比大小,就需要对begin,end的取值作处理,以防出现异常,对不同的异常打印出不同的异常信息可以让调用者了解是出现begin>=end,begin<0,还是end>arr.length异常。
Q4.函数题目4-1(多种异常的捕获)
4.1 截图你的提交结果(出现学号)
4.2 一个try块中如果可能抛出多种异常,捕获时需要注意些什么?
- 子类异常捕获必须在父类异常前面,否则会出现编译错误。
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关闭资源。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
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(FileNotFoundException e){
System.out.println(e);
}catch(IOException e){
System.out.println(e);
}finally{
if(fis!=null)
try{
fis.close();
}catch(IOException e){
System.out.println(e);
}
}
System.out.println(Arrays.toString(content));//打印数组内容
}
}
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(IOException e){
System.out.println(e);
}
Q6.重点考核:使用异常改进你的购物车系统(未提交,得分不超过6分)
举至少两个例子说明你是如何使用异常处理机制让你的程序变得更健壮。
说明要包含2个部分:1. 问题说明(哪里会碰到异常)。2.解决方案(关键代码)
//输入购买件数可能输入的非法数据
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
GoodsDao goodDao = new Jtable(jTable1);
try{
count=Integer.parseInt(jTextField1.getText());
}catch(NumberFormatException e){
System.out.println(e);
}
...
}
//是否成功加入购物车
try{ clothes[0]=good.getName();
clothes[1]=Double.toString(good.getPrice());
clothes[2]=Integer.toString(good.getNum());
}catch(Exception e){
System.out.println(e);
}
//从文件导入数据
try( FileInputStream fis= new FileInputStream("clothes.txt")){
int bytesAvailabe = fis.available();//获得该文件可用的字节数
if(bytesAvailabe>0){
content = new byte[bytesAvailabe];//创建可容纳文件大小的数组
fis.read(content);//将文件内容读入数组
}
}catch(FileNotFoundException e){
System.out.println(e);
}catch(IOException e){
System.out.println(e);
}
System.out.println(Arrays.toString(content));//打印数组内容
}
**Q7.选做:JavaFX入门
如果未完成作业1、2的先完成1、2。贴图展示。如果已完成作业1、2的请完成作业3。内有代码,可在其上进行适当的改造。建议按照里面的教程,从头到尾自己搭建。 **
**Q8.选做:课外阅读
JavaTutorial中Questions and Exercises
写出读后感 **