Job 10-exception
1. Summary of this week's study1.1 Summarize the relevant contents of the collection in the way you like (mind map or other).
2. Written work
This PTA work problem set异常
1. Common Exceptions
7-1 answer with topic set
1.1 What exceptions do you often have in code that you have written before, and what do you need to capture (why)? What should be avoided?
?? Previously written code often occurs with ArrayIndexOutOfBoundsException
exceptions that do not need to be captured and should not be inherited directly Exception
. So there is no need to capture. Arrays are generally out of bounds when the program declares control, so pay more attention when declaring control to reduce the incidence of this error.
1.2 What kind of exception requires the user to be sure to use capture processing?
?? When a subclass that inherits directly from it Exception
must be captured for processing.
2. Handle exceptions to make your program more robust
Topic 7-2
2.1 Experimental summary. And answer: How can you make your program more robust?
?? difficulty, which can be in the array when the error may be, because the topic is required to order the input integer type, if the input non-integer characters, will be wrong, so in that code to add Try-catch, the exception to capture. There is also when the error, subscript to use i++, so do not miss out.
?? When writing code, consider whether there is a problem in one place, and when there may be an exception, you can use Try-catch to handle the exception.
3. Throw and throws
Topic 7-3
Read the Integer.parsetInt
source code
3.1
Integer.parsetInt
What is the benefit of having a large number of code throwing exceptions at the outset?
?? That is direct, not like C, error generally return 1, but do not know where the specific error, but if Java in the beginning to throw all the exception problems, change it up fast.
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?
?? The corresponding exception is first printed, and then the output error is indicated, such as when the input begin>end, the exception is thrown, and the value of IllegalArgumentException
begin is greater than the value of end.
4. Improve Arrayintegerstack with exception
Topic 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?
?? Directly obvious, will not let the program crash, throw an exception, I know where there is a problem, how to change, than the simple return error value more efficient.
4.2 If an inner code inside a method throws an exception of type RuntimeException, then the method declaration should use the
throws
keyword, if you use the
throws
Keyword declares the exception thrown by this method, what benefits can it bring to us?
?? No, because the throws keyword is used to create an exception in a method, and the method does not internally know how to handle the exception. And the problem has thrown the runtimeexception type of exception, is to know what is abnormal.
?? If there is checked exception in the code, but it is troublesome to write the Try-catch statement, this time throws the exception in main.
5. Function questions-capture of multiple exceptions
Topic 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?
?? If you can throw multiple exceptions in a try block, each catch block catches a different exception, and if there may be an inheritance relationship between exceptions, be aware that the subclass exception must precede the parent class exception. Otherwise, the subclass exception cannot be exported.
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?
?? Exceptions in a catch block must not have an inheritance relationship
6. Add exception handling to the following code
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 Correct the code and add the following functions. When the file is not found, you need to prompt the user
找不到文件xxx
, re-enter the file name, and then try to open it again. Prompt if it is a different exception
打开或读取文件失败!
。
Note 1: There are several methods inside that can throw an exception.
feature 2: need to add finally
close file. Always prompt, regardless of whether the above code produces an exception 关闭文件ing
. If closing the file fails, prompt关闭文件失败!
6.2 Combined Problem Sets
6-2
Code, what do you want to put in the finally block? Why? What do I need to be aware of when closing resources using finally?
?? The finally block executes some code that frees the resource, and the code in the finally block is sure to execute. Prevents an exception when resources are closed, you can join a try-catch catch close exception in a finally block
6.3 Using the Java7 in the
try-with-resources
To rewrite the above code to automatically close the resource. What are the benefits of this approach?
?? You can resolve the problem of abnormally bloated statements, which is a bit clumsy when closing resources in the finally statement on 6.2. Using try-with-resources syntax is a lot more concise.
7. Object-oriented design job-Library management system (group completion, no more than 3 students per group)
Log in to lib.jmu.edu.cnto 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?
?? Librarians and students
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.
?? Log in, register, view information, search for books, borrow, book, return
principal |
Module |
Zheng Yupo |
Search for a book, borrow it, return it |
Jianheng |
Register, login, view information |
7.3 Main class design and class diagram of the system (available) 7.4 How you plan to store library information, address information, reader information, and more.
?? Use one file to store the book information and modify it to resolve the information and to store the reader information in another file.
3. Code Cloud and PTA
Topic Set: Exceptions
3.1. Code cloud codes Submission record
- In the Code cloud Project, select statistics-commits history-set time period, and then search for and
3.2 PTA Problem set complete situation diagram
Two graphs are required (1. Ranking chart. 2.PTA Submission list diagram)
3.3 count The amount of code completed this week
The weekly code statistics need to be fused into a single table.
Week Time |
Total code Amount |
New Code Volume |
total number of files |
number of new files |
1 |
0 |
0 |
0 |
0 |
2 |
571 |
571 |
19 |
19 |
3 |
1029 |
458 |
20 |
1 |
4 |
1029 |
0 |
20 |
0 |
5 |
1359 |
330 |
25 |
5 |
6 |
2061 |
702 |
35 |
10 |
7 |
2653 |
592 |
42 |
7 |
8 |
3315 |
36W |
48 |
6 |
9 |
4171 |
95W |
54 |
6 |
10 |
4569 |
398 |
59 |
5 |
11 |
4887 |
318 |
70 |
11 |
201621123028 Java programming 10th Week of study summary