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

时间:2021-09-23 10:46:26

1. 本周学习总结

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

2. 书面作业

本次PTA作业题集异常

1.常用异常

题目5-1

1.1 截图你的提交结果(出现学号)

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

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

答: 经常会出现 ArrayIndexOutOfBoundsException 数组越界异常,但是它不需要捕获,因为该异常是RuntimeException,属于unchecked exception。
想要避免该异常,只需要改进代码就可以。

1.3 什么样的异常要求用户一定要使用捕获处理?

答:除了Error与RuntimeException及其子类以外的异常代码中必须try-catch来捕获异常。

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

题目5-2

2.1 截图你的提交结果(出现学号)

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

2.2 实验总结

  • 这道题应该注意的是当发现输入不为数字抛出异常后,数组下标应该减一,避免让其加到数组里。

3.throw与throws

题目5-3

3.1 截图你的提交结果(出现学号)

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

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 s 为null时,抛出异常NumberFormatException,输出“null”
} if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");//当radix < Character.MIN_RADIX时,抛出异常NumberFormatException及语句。
} if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");//当radix > Character.MIN_RADIX时,抛出异常NumberFormatException及语句。
} 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 != '+')//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;
}
  • 根据上述代码可知:抛出异常时需要传递给使用者出错的原因,不同的错误原因抛出不同的错误,让使用者及时更改。

4.函数题

题目4-1(多种异常的捕获)

4.1 截图你的提交结果(出现学号)

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

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关闭资源。

代码和运行结果为:

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

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

代码及其运行结果为:

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

6.重点考核:使用异常改进你的购物车系统(未提交,得分不超过6分)

举至少两个例子说明你是如何使用异常处理机制让你的程序变得更健壮。

说明要包含2个部分:1. 问题说明(哪里会碰到异常)。2.解决方案(关键代码)

1.添加商品数量时,手误输入的不是数字,抛出异常。

 System.out.println("请您输入要添加的数量");
try{
int y=sc.nextInt();
cart[0].add(y, o);
}catch (InputMismatchException e){
System.out.println(e);
}

2.输入不在列表中的数字选择时,抛出异常。

System.out.println("输入想要购买的商品");
System.out.println("1.书");
System.out.println("2.食品");
try{
int n=sc.nextInt();
if(n==1)
{System.out.println("1"+book[0]);
System.out.println("2"+book[1]);
System.out.println("输入有效数字选择商品");
int i=sc.nextInt();
System.out.println("输入想要购买数量");
int x=sc.nextInt();
Shoppingcart p=new Shoppingcart(book[i-1],x);
cart[0]=p;
}
else if(n==2)
{System.out.println("1"+food[0]);
System.out.println("2"+food[1]);}
}catch (InputMismatchException e){
System.out.println(e);
}

3. 码云上代码提交记录

题目集:异常

3.1. 码云代码提交记录

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