1. Study summary 1.1 This week summarize anomalies in the way you like (mind mapping or other).
Mind Mapping:
2. Written work
This PTA job problem set anomaly
2.1. Common exceptions
Combined Topic 7-1 Answer
2.1.1 What anomalies do you often have in your previously written code, and what do you need to capture (why)? What should be avoided?
Exception: ArrayIndexOutOfBoundsException
Capture: No, arrayindexoutofboundsexception exceptions belong to subclasses of RuntimeException exceptions.
Avoid: Improve the code, do not let the array subscript out of bounds.
Exception: ClassCastException
Capture: No, classcastexception exceptions belong to subclasses of RuntimeException exceptions.
Avoid: Improve the code and try not to make the wrong type conversions.
2.1.2 What kind of exception is required for users to be sure to use capture processing?
Be sure to catch unhandled exceptions: except for exceptions Erro and RuntimeException and runtimeexception subclasses, you use capture processing.
2.2. Handle exceptions to make your program more robust
Topic 7-2
2.2.1 Experimental summary. And answer: How can you make your program more robust?
Summary: the teacher in the experiment class to give some code, need to do is to consider the array loop when the i++ to put the position. At first I did not consider where i++ to put, directly in the catch exception, later found the problem, and then follow the instructions of the teacher to do the processing, put i++ in the try.
Answer: using the exception handling input mechanism
2.3. Throw and throws
Topic 7-3
Read Integer.parsetint source code
3.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?
Benefits: Read the source code can be found Integer.parsetint may have an exception, the beginning of the throws exception, you can not write the Try-catch statement block behind, you can simplify the code.
3.3.2 combined with its own program and 3.1, the analysis of the method you write to throw an exception is generally required to pass to the caller what information?
Summary: in writing this question, the feeling is basically relatively simple, is the processing of the space ... Tried it several times.
Information:(refer to PPT)
1. When an exception occurs, what is wrong with the system.
2. The exception is whether you want someone to catch the deal or not to capture it.
2.4. Improve Arrayintegerstack with exception
Topic 6-3
2.4.1 combined 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?
Benefit: If the program is running in error, the program can only be forcibly terminated by system intervention. However, if you use a method that throws an exception, there is no way to force the system to terminate the program.
Advantages: When doing 6-3 feel in the program add exception handling, make the program run more robust, than the simple return error value, provide the caller more information, such as what is the specific cause of the exception and so on.
2.4.2 If the inner code of a method throws an exception of type runtimeexception, then 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?
Answer: RuntimeException types of exceptions do not need to be captured, so I think it is OK not to declare throws ... The benefits seem to have been unexpectedly, perhaps, to make the program more robust?
2.5. Function questions-capture of multiple exceptions
Topic 6-1
2.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?
Attention:
2.5. If you can throw multiple exceptions in 21 try blocks, what do you need to be aware of when using Java8 's multiple exception capture syntax?
Attention:
Exceptions in a catch block must not have an inheritance relationship
2.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));//打印数组内容
2.6.1 Correct the code so that it can function properly. Note 1: There are several methods inside that can throw an exception. Note 2: To use finally to close the resource.
Correction Code:
2.6.2 Combining the 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?
Action: operations that need to release resources (such as shutting down a database, etc.)
Cause: When an exception occurs, code that throws an exception is not executed, and if the program opens the associated resource before the exception occurs, the code may not close the resource properly, and finally the statement block code is executed, so you can use finally to free up resources.
Attention:
1. First to determine whether it is null
2. From the process of improving the code, it can be seen that even when resources are closed, exceptions can occur and need to be captured.
2.6.3 uses try-with-resources in Java7 to rewrite the above code to automatically close the resource. What are the benefits of this approach?
PPT information:
Rewrite code:
Benefit: Comparing the original finally statement block, it can be found that using the above code to automate the shutdown of resources can reduce the amount of code and avoid the abnormally bloated finally statement.
2.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.
2.7.1 who is the user of this system?
Users : Librarian, user
2.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.
Because the former partners do not do this piece of content, so I intend to write a user-oriented module, although I feel that the book entry should be the librarian do things, but for the normal operation of the program. I have to add some to it first. The form is as follows:
function Module |
principal |
Book entry, search, borrowing, returning books |
Wu IELTS |
2.7.3 the main class design and class diagram of the system (available)
Library user class, book category
class Diagram:(now not specific to think clearly, draw a simple point, it seems that the book will have an ISBN code, I first add in ...) )
2.7.4 how you plan to store library information, address information, and reader information.
A : the storage of books information should follow the previous practice of similar shopping cart system, first define a class, and then use an array to implement, check the words to choose to use an array to traverse it, and now also did not write code, probably think about it. Now is how to check the reader's current book lending situation, should be to write a personal borrowing situation, the implementation of this has not yet thought well ....
2.9. Select: Use the static Code scanning Tool to scan your own shopping cart code 2.9.1 analyze the problems in your code that arise in the specification.
problem:(it seems that most of the variable names are not canonical.) )
2.9.2 think about how to solve these problems.
solve: give the variable name of time or to pay attention to the specific meaning, do not casually name, such as the first letter of the class name to capitalize ... Another problem is that "x should be the parameter of equals instead of the caller", this I am a bit ignorant, do not know what meaning ....
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)
Ranking chart:
Submit a list:
{{Uploading-image-40387.png (uploading ...)}}
{{Uploading-image-470074.png (uploading ...)}}
3.3 Count the amount of code completed this week
The weekly code statistics need to be fused into a single table.
Week number |
Number of rows |
number of new rows |
Number of files |
number of new files |
1 |
57 |
57 |
5027 |
5027 |
2 |
400 |
400 |
10 |
10 |
3 |
508 |
508 |
23 |
13 |
5 |
609 |
609 |
31 |
8 |
6 |
612 |
612 |
40 |
9 |
7 |
584 |
584 |
46 |
6 |
8 |
432 |
432 |
50 |
4 |
9 |
620 |
620 |
56 |
6 |
10 |
468 |
468 |
60 |
4 |
11 |
476 |
476 |
67 |
7 |
Option: 4. Expand
Extracurricular exercises
Javatutorial in questions and exercises
Practice Summary
Extracurricular reading
Select one of the following articles to read and enumerate some of the best practices for exception handling that you can understand.
Best Practices for Exception handling
Exception-handling Antipatterns Blog
The Exceptions debate
201621123062 Summary of the tenth Zhou of Java programming