1. 本周学习总结

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


2. 书面作业

1.常用异常

题目5-1

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

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

NullPointerExceptin.在编写代码测试的时候可能会发生数组越界,系统直接抛出错误并终止程序。通过捕获异常,输出异常提示,可以继续执行下面的程序。

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

除了Error与RuntimeException及其子类以外的异常都是Checked Exception,Checked Exception异常代码中必须try-catch处理。


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

题目5-2

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

2.2 实验总结

PPT上有相关内容,就注意在捕获异常之后要对下标进行自减处理。


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

首先,Integer.parsetInt判断输入字符串是否为空,进制数是否越界,并且给出了每个不同位置的字符可能出现的异常,并将对应的异常抛出。抛出异常时需要传递给我们出现的异常为什么,以及输出我们在抛出异常时赋予他们的值或我们想要的输出格式,导致异常的原因。


4.函数题

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

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

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

如果一个try模块后面跟着多个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关闭资源。

 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));//打印数组内容
fis.close();
}
catch(IOException 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) {
System.out.println(e);
} catch (IOException e) {
System.out.println(e);
}

3. 码云上代码提交记录

题目集:异常

3.1. 码云代码提交记录

在码云的项目中,依次选择“统计-Commits历史-设置时间段”, 然后搜索并截图

最新文章

  1. 解决checkbox的attr(checked)一直为undefined问题
  2. jquery实现可拖拽的div
  3. jQuery中的事件与动画 (你的明天Via Via)
  4. Esper系列(十一)NamedWindow语法Merge、Queries、Indexing、Dropping
  5. 【python自动化第七篇:面向对象进阶】
  6. DHTML【6】--CSS
  7. Delphi xe7并行编程快速入门(三篇)
  8. VMware设置NAT网络
  9. PAT (Advanced Level) 1067. Sort with Swap(0,*) (25)
  10. 【原创】驱动加载之CreateService
  11. 【转】PEP8 规范
  12. Nginx的核心功能及应用实战
  13. Gogoing的NABCD
  14. hdu 5301 Buildings (2015多校第二场第2题) 简单模拟
  15. awk 取列后对数值进行判断取出大于1的数值
  16. Jenkins时区设置
  17. 网页中输出漂亮格式的Php数组神器
  18. Android6.0指纹识别开发
  19. 理解shell中的atime,mtime,ctime
  20. 【LeetCode】【定制版排序】Sort Colors

热门文章

  1. TCP/IP小记
  2. .Net Core 2.0生态(3):ASP.NET Core 2.0 特性介绍和使用指南
  3. 字符串拼接data-id时注意事项
  4. php面试题汇总四(基础篇附答案)
  5. 使用proxool连接池配置教程
  6. JSON数据表示格式简介(JavaScript对象表示法)
  7. shell中while循环的陷阱
  8. Centos7.2 编译安装方式搭建 phpMyAdmin
  9. RSA,Miller-Rabin素数测试的源流及其证明
  10. C# 实现模拟登录功能,实现公共类分享。