1. 本周学习总结

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

2. 书面作业

本次PTA作业题集异常

1.常用异常

题目5-1

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

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

ArrayIndexOutOfBoundsException,NullPointerException,ClassCastException等,不需要捕获,因为它们是uncheckedException,要在逻辑上避免,通过改进代码避免出现此类错误。

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

自定义的直接继承自Exceptino的异常,就是除了RuntimeException及其子类以外,其他的Exception类及其子类异常要求用户一定要使用捕获处理。

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

题目5-2

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

2.2 实验总结

出现NumberFormatException ,要对该异常进行捕获,在捕获异常之后要对下标进行自减处理。

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

输入不同会有不同的异常,抛出异常时需要传递出现异常的原因,提醒调用者应当从哪个方向修改。5-3中因为开始位置大于结束位置抛出异常时,会说明 begin:X >= end:Y 。调用者就会依据这个提醒修改begin或end值使 begin < end。

4.函数题

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

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

4.2 一个try块中如果可能抛出多种异常,捕获时需要注意些什么?

对于可能抛出多个异常的代码块,可以不对每个异常都提供一个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(FileNotFoundException e){System.out.println(e);}
catch(IOException e){System.out.println(e);}
finally
{
if(fis!=null)
try{
fis.close();
}
catch(Exception e){System.out.println(e);}
}
}

5.2 使用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);//将文件内容读入数组
}
System.out.println(Arrays.toString(content));//打印数组内容
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}

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

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

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

7.选做:JavaFX入门

如果未完成作业1、2的先完成1、2。贴图展示。如果已完成作业1、2的请完成作业3。内有代码,可在其上进行适当的改造。建议按照里面的教程,从头到尾自己搭建。

8.选做:课外练习

JavaTutorial中Questions and Exercises

练习总结

3. 码云上代码提交记录

题目集:异常

3.1. 码云代码提交记录

选做:4. 课外阅读

任选下面一篇文章阅读,列举出几点自己能理解的异常处理最佳实践。

Best Practices for Exception Handling

Exception-Handling Antipatterns Blog

The exceptions debate

最新文章

  1. WinForm DataGridView分页功能
  2. mysql中的游标使用案例
  3. NSDate和NSString相互转换
  4. Javascript事件模型系列(二)事件的捕获-冒泡机制及事件委托机制
  5. [Asp.net 5] DependencyInjection项目代码分析4-微软的实现(3)
  6. python—面向对象编程
  7. JAVA中toString方法的作用
  8. 2013年优秀jQuery插件
  9. The type or namespace name &#39;Script&#39; does not exist in the namespace &#39;System.Web&#39; (are you missing an assembly reference?)
  10. fzu 1753 Another Easy Problem
  11. portal安装常见问题
  12. Struts1、2种如何防止表单重复提交和两者的区别
  13. SAP MM 预留单据的历史修改记录?
  14. js 实现数据结构 -- 集合
  15. 浅析 java ArrayList
  16. kebab HDU - 2883(按时间段建点)
  17. 双系统安装Ubuntu
  18. DNSlog实现Mysql注入
  19. SharePoint 2013 页面中window/document.onload/ready 事件不能触发的解决方案
  20. 十分钟用 Node 命令行工具打造 react-cli 脚手架

热门文章

  1. opencv 基本绘图函数
  2. SQL 三种基本Join
  3. Android studio 使用问题汇总
  4. Elastic Stack
  5. 一篇深入剖析PCA的好文
  6. ASP.NET Core 运行原理解剖[1]:Hosting
  7. 1&gt;MSBUILD : cordova-build error BLD401: 错误: BLD00401: 找不到模块“C:\Users\z-pc\AppData\Roaming\npm\node_modules\vs-tac\app.js”。请转到“工具”--&gt;“选项”--&gt;“Apache Cordova 工具”--&gt;“Cordova 工具”--&gt;“清除 Cordova 缓存”,然后尝试重新生成
  8. 【Ubuntu16]】ufw
  9. OminiMarkupPreview快捷键
  10. Spring Data Jpa(Hibernate) OneToMany