201621123061《Java程序设计》第10次学习总结

1. 本周学习总结

1.1 以你喜欢的方式(思惟导图或其余)概括总结异常相关内容。

2. 书面做业

本次PTA做业题集异常javascript

1. 经常使用异常

结合题集题目7-1回答java

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源代码app

3.1 Integer.parsetInt一开始就有大量的抛出异常的代码,这种作法有什么好处?

这样能够不用绞尽脑汁去考虑各类错误,能够为处理某一类错误提供有效的解决方法,使编程效率提升。dom

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时会抛出异常,且在异常中写明异常的缘由。让调用者明白为何产生异常,从而寻找方法。ide

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
练习总结

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

修改以下:

相关文章
相关标签/搜索