Null pointer exception NullPointerException
Do not need to capture, they are inherited from RuntimeException, is unchecked Exception, write code to pay more attention to
1.2 What kind of exception requires the user to be sure to use capture processing?Any exception that inherits directly from exception needs to be captured
2. Handle exceptions to make your program more robustTopic 7-2
2.1 Experimental summary. And answer: How can you make your program more robust?Input, it is possible to enter a non-integer string, this time you need to output the exception information, and then re-enter.
To put the input string into a try block, catch the exception
3. Throw and throwsTopic 7-3
Read Integer.parsetint source code
3.1 Integer.parsetint There is a lot of code thrown out of the exception at the outset, what is the benefit of this practice?Allows the user to discover the cause of the exception
3.2 What information do you usually need to pass to the caller when you write your own program with 3.1 and analyze your own method of throwing an exception?A illegalargumentexception exception is generated that needs to be passed to the caller to produce an exception, for example: Begin<end, begin must not be less than 0, end must not be greater than arr.length
4. Improve Arrayintegerstack with exceptionTopic 6-3
4.1 In combination with 6-3 code, how does it benefit to use an exception-throwing method to represent a program run-time error? What is the advantage of returning an error value more than a simple one?The use of throwing exceptions means that a program run-time error does not cause the program to crash, but instead continues to run and throws the exception so that we can find the problem more intuitively
4.2 If the inner code of a method throws an exception of type RuntimeException, does the method declaration use the throws keyword, and if the exception that is thrown by the method is declared using the throws keyword, what benefit can it bring us?RuntimeException types of exceptions belong to unchecked Exception, and you do not need to use the throws keyword to declare
If you declare an exception of type runtimeexception with the Throw keyword, you may throw an exception as a hint
5. Function questions-capture of multiple exceptionsTopic 6-1
5.1 Combined with 6-1 code, answer: If you can throw multiple exceptions in a try block, and there may be an inheritance relationship between exceptions, what do you need to be aware of when capturing?When there is an inheritance relationship between exceptions, you should catch the subclass exception before catching the parent class exception, or the exception of the subclass will not be captured
5.2 If you can throw multiple exceptions in a try block, what do you need to be aware of when using Java8 's multiple exception capture syntax?The child class exception is captured before the parent class
6. Add exception handling to the following codebyte[] 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));//print array contents
6.1 Correct the code and add the following functions. When the file cannot be found, you need to prompt the user not to find the file xxx, retype the file name, and then try to open it again. If it is another exception, prompt to open or read the file failed!.Note 1: There are several methods inside that can throw an exception.
Feature 2: You need to add a finally close file. Regardless of whether the above code produces an exception, always be prompted to close the file ing. If closing the file fails, the prompt to close the file failed!
public class Main1 {public static void main(String[] args) { 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);//将文件内容读入数组 } }catch (FileNotFoundException e){ System.out.println(e); System.out.println("找不到文件testfis.txt,请重新输入文件名"); }catch (IOException e){ System.out.println(e); System.out.println("打开或读取文件失败!"); } finally { try { fis.close(); System.out.println("关闭文件ing"); }catch (Exception e){ System.out.println(e); System.out.println("关闭文件失败"); } } System.out.println(Arrays.toString(content));//打印数组内容}}
6.2 Combining the title set 6-2 code, what kind of action to put in the finally block? Why? What do I need to be aware of when closing resources using finally?The operation that frees the resource should be placed in the finally block, because the statement of the finally block is always executed, and the statement executed inside can also produce an exception when using finally to close the resource, so it is also try-catch handled.
6.3 Use try-with-resources in Java7 to rewrite the above code to automatically close the resource. What are the benefits of this approach?try(FileInputStream fis = new FileInputStream("testfis.txt")){ int bytesAvailabe = fis.available();//获得该文件可用的字节数 if(bytesAvailabe>0){ content = new byte[bytesAvailabe];//创建可容纳文件大小的数组 fis.read(content);//将文件内容读入数组 } }catch(Exception e){ System.out.println(e); }System.out.println(Arrays.toString(content));}
You do not need to close resources separately in the finally block
7. Object-oriented design job-Library management system (group completion, no more than 3 students per group)Log in to lib.jmu.edu.cn to search for books. Then log in to the library information system to view my library. If you want to implement a book borrowing system, try using object-oriented modeling.
7.1 Who is the user of this system?Student, teacher, administrator
7.2 Main function modules (not too many) and the owner of each module. Next week everyone is going to submit their own module code and run the video. Student, Teacher: