201621123061《Java程序设计》第10次学习总结
1. 本周学习总结
1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容。

2. 书面作业
本次PTA作业题集异常
1. 常用异常
结合题集题目7-1回答
1.1 自己以前编写的代码中经常出现什么异常、需要捕获吗(为什么)?应如何避免?
- 数组越界的问题;还有空指针,比如一个数组是空的,却直接要算数组的长度,这时候就会出现空指针问题;强制转换会出现ClassCastException;
- 这些异常一般不需要,因为是在运行的时候出现的异常。
- 使用try-catch处理异常。
1.2 什么样的异常要求用户一定要使用捕获处理?
除了error和RuntimeException之外的异常都需要捕获。
2. 处理异常使你的程序更加健壮
题集题目7-2
2.1 实验总结。并回答:怎么样才能让你的程序更加健壮?
try{
String input=sc.next();
arr[i]=Integer.parseInt(input);
i++;
}catch(Exception e){
System.out.println(e);
}
在try程序块中放可能会出现异常的代码,当输入非整数的字符串时,会出现异常,则用catch捕获。
要处理异常可以使程序更加健壮。
3. throw与throws
题集题目7-3
阅读Integer.parsetInt源代码
3.1 Integer.parsetInt一开始就有大量的抛出异常的代码,这种做法有什么好处?
这样可以不用绞尽脑汁去考虑各种错误,可以为处理某一类错误提供有效的解决方法,使编程效率提高。
3.2 结合自己编写的程序与3.1,分析自己编写的方法抛出异常时一般需要传递给调用者一些什么信息?
public static double findMax(double[] arr,int begin, int end) throws IllegalArgumentException{
if(begin>=end)
throw new IllegalArgumentException("begin:"+begin+">="+"end:"+end);
else if(begin<0)
throw new IllegalArgumentException("begin:"+begin+"<0");
else if(end>arr.length)
throw new IllegalArgumentException("end:"+end+">"+"arr.length");
double max=arr[begin+1];//findMax方法用来返回arr数组中在下标begin(不包含begin)与end-1之间(包括end-1)的最大值。
for(int i=begin+1;i<end;i++){
if(max<arr[i]){
max=arr[i];
}
}
return max;
}
当begin>=end,begin<0,end>arr.length时会抛出异常,且在异常中写明异常的原因。让调用者明白为什么产生异常,从而寻找方法。
4. 用异常改进ArrayIntegerStack
题集题目6-3
4.1 结合6-3代码,回答使用抛出异常的方式代表程序运行时出错有什么好处?比单纯的返回错误值,有何优点?
好处:出错信息可以更详细,让用户更好发现错误。在本题中方法push(),如果栈满就抛出FullStackException,方法pop()和peek()要判断栈空,如果栈空就要抛出EmptyStackException,单纯返回错误值,信息太过简略,而且很有可能错误代码也是正确的结果(比如-1),而抛出异常可以避免这些问题。
4.2 如果一个方法内部的内码抛出的是RuntimeException类型的异常,那么方法声明是否应该使用throws关键字,如果使用throws关键字声明该方法抛出的异常,能给我们带来什么好处吗?
- 可以不throws,因为RuntimeException不是必须被捕获的,不一定要抛出异常。
- 好处:代码中含有checked Exception,使用throws关键字抛出异常,可以不用辛辛苦苦地写try-catch子句。
5. 函数题-多种异常的捕获
题集题目6-1
5.1 结合6-1代码,回答:一个try块中如果可能抛出多种异常,且异常之间可能有继承关系,捕获时需要注意些什么?
子类异常一定要放在父类异常之前。
5.2 一个try块中如果可能抛出多种异常,使用Java8的多重异常捕获语法需要注意些什么?
try块中后要跟多个catch块,catch块中的异常不得有继承关系。
6. 为如下代码加上异常处理
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));//打印数组内容
6.1 改正代码,并增加如下功能。当找不到文件时,需提示用户找不到文件xxx,请重新输入文件名,然后尝试重新打开。 如果是其他异常则提示打开或读取文件失败!
注1:里面有多个方法均可能抛出异常。
功能2:需要添加finally关闭文件。无论上面的代码是否产生异常,总要提示关闭文件ing。如果关闭文件失败,提示关闭文件失败!
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(FileNotFoundException | SizeDetermineFailed | MemoryAllocateFailed | ReadFailed e){
e.getStackTrace();
}finally{
if (fis != null) {
try { //关闭资源也有可能出错
fis.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("找不到文件xxx,请重新输入文件名");
}catch(Exception e){
System.out.println("打开或读取文件失败!");
}
}
}
System.out.println(Arrays.toString(content));//打印数组内容
6.2 结合题集6-2代码,要将什么样操作放在finally块?为什么?使用finally关闭资源需要注意一些什么?
将关闭资源的操作放在finally块,因为finally块里的代码始终都要执行。有时候在关闭资源的时候也会遇到异常,这时候也要捕获。
6.3 使用Java7中的try-with-resources来改写上述代码实现自动关闭资源。简述这种方法有何好处?
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){
e.printStackTrace();
}
System.out.println(Arrays.toString(content));
7. 面向对象设计作业-图书馆管理系统(分组完成,每组不超过3个同学)
登录lib.jmu.edu.cn,对图书进行搜索。然后登录图书馆信息系统,查看我的图书馆。如果让你实现一个图书借阅系统,尝试使用面向对象建模。
7.1 该系统的使用者有谁?
集美大学的教师和学生,以及图书系统管理员。
7.2 主要功能模块(不要太多)及每个模块的负责人。下周每个人要提交自己负责的模块代码及运行视频。
暂时打算自己完成,算给自己一个挑战吧。。。
7.3 该系统的主要的类设计及类图(可用)

7.4 你准备如何存储图书信息、解决信息、读者信息等。
暂时打算分成三个主要模块解决,分别为用户登录,用户管理模块,借阅管理模块,图书管理模块,和退出系统。系统需要添加图书、修改图书、删除图书、添加用户、修改用户、删除用户、借阅图书、归还图书和查阅图书等过程。为了安全和方便,打算用数据库来存储这些信息,可以采用表的形式来描述图书信息、解决信息、读者信息。
8. 选做:使用异常改进你的购物车系统
举1个例子说明你是如何使用异常处理机制让你的程序变得更健壮。
说明要包含2个部分:1. 问题说明(哪里会碰到异常)。2.解决方案(关键代码)
1.在登录的时候,如果输入未存在的账号,则会无响应。
2.抛出异常
关键代码:
try{
if (Shopping.this.map.get(userInput.getText()).equals(s)) {
new Menu().setVisible(true);
Login.this.dispose();
}
} catch(Exception e){
JOptionPane.showMessageDialog(null, "该账号不存在,请重新输入!");
}
3.码云及PTA
题目集:异常
3.1. 码云代码提交记录
在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

3.2 截图PTA题集完成情况图
需要有两张图(1. 排名图。2.PTA提交列表图)


3.3 统计本周完成的代码量
需要将每周的代码统计情况融合到一张表中。

| 周次 | 行数 | 新增行数 | 文件数 | 新增文件数 |
|---|---|---|---|---|
| 1 | 91 | 91 | 5 | 5 |
| 2 | 504 | 413 | 18 | 13 |
| 3 | 1092 | 588 | 28 | 10 |
| 5 | 1158 | 129 | 34 | 6 |
| 6 | 1539 | 381 | 40 | 6 |
| 7 | 2023 | 484 | 49 | 9 |
| 8 | 2477 | 454 | 57 | 8 |
| 9 | 2709 | 232 | 63 | 6 |
| 10 | 3156 | 447 | 70 | 7 |
| 11 | 3531 | 375 | 79 | 9 |
4. 拓展
课外练习
JavaTutorial中Questions and Exercises
练习总结
1.Is the following code legal?
try {
} finally {
}
- Yes
2.What exception types can be caught by the following handler?
catch (Exception e) {
}
What is wrong with using this type of exception handler?
- All exceptions can be captured because all exceptions are inherited Exception.
- The disadvantage is that you can't do specific processing according to specific exceptions.
3.Is there anything wrong with the following exception handler as written? Will this code compile ?
try {
} catch (Exception e) {
} catch (ArithmeticException a) {
}
- Exception is the parent of all exceptions, so in the first catch block, the Exception will be captured.The second catch block will never execute.Compilation cannot pass.
4.Match each situation in the first list with an item in the second list.
int[] A;
A[0] = 0;
The JVM starts running your program, but the JVM can't find the Java platform classes. (The Java platform classes reside in classes.zip or rt.jar.)
A program is reading a stream and reaches the end of stream marker.
Before closing the stream and after reaching the end of stream marker, a program tries to read the stream again.
_b_error
_d_checked exception
_a_compile error
_c_no exception
Exercises
1.Add a readList method to ListOfNumbers.java. This method should read in int values from a file, print each value, and append them to the end of the vector. You should catch all appropriate errors. You will also need a text file containing numbers to read in.
2.Modify the following cat method so that it will compile.
public static void cat(File file) {
RandomAccessFile input = null;
String line = null;
try {
input = new RandomAccessFile(file, "r");
while ((line = input.readLine()) != null) {
System.out.println(line);
}
return;
} finally {
if (input != null) {
input.close();
}
}
}
修改如下:

浙公网安备 33010602011771号