1. Summary of this week's study
2. Written assignment 1. Common exceptions 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?
- The most frequent errors include arrayindexoutofboundsexception (array out of bounds), nullpointerexception (null pointer), classcastexception (coercion type conversion error), IllegalArgumentException (illegal parameter), the above exception does not need to capture, all belong to Uncheckedexception.
1.2 What kind of exception requires the user to be sure to use capture processing?
- A custom exception that inherits from the exception class requires capture processing.
2. Handle exceptions to make your program more robust 2.1 experimental summary. And answer: How can you make your program more robust?
- 1. Release the reference to the useless object as soon as possible. 2. The definition string should use string str= "Hello" as much as possible; The form. 3. Use as few static variables as possible. 4. Try to avoid creating and initializing a large number of objects in the class's constructor. 5. Do not create an object in a frequently invoked method.
3. Throw and throws3.1
Integer.parsetInt
What is the benefit of having a large number of code throwing exceptions at the outset?
- 1. The error message can be very detailed. 2. Allow users to effectively discover errors. 3. The error code is separated from the error handling.
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 result of the exception is displayed, telling the programmer the reason for the exception, for example, in 7-3, the problem shows us the
java.lang.IllegalArgumentException: begin:3 >= end:3
location of the error.
4. Using exception improvement ArrayIntegerStack4.1 in conjunction 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?
- Errors can be found effectively, separating the error code from the error handling. Error messages can be very detailed (packaged as objects)
4.2 When should I use the Throw keyword?
- For a run exception, we should not use throws to handle, for the checked exception, compile time cannot pass, then use throw or try. Catch... To deal with.
4.3 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是那些可能在Java虚拟机正常运行期间抛出的异常的超类。可能在执行方法期间抛出但未被捕获的RuntimeException 的任何子类都无需在throws子句中进行声明。它是Exception的子类。
Throws is used to declare what exceptions the method might throw, after the method name, the syntax format is: throws exception type 1, exception type 2 ... Exception type N. can enhance the robustness of the program.
5. Function questions-Multiple exception captures 5.1 in combination 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?
- Exception captures for subclasses must precede the parent class. Or the program will crash.
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?
- As much as possible to catch specific exceptions, if there is an exception between the parent class and the subclass of the relationship, then the subclass is required, the parent class is next.
6. Add exception handling for the following code 6.1 to 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.
6.2 Ending handling exceptions and using finally close resources what do you need to be aware of?
- Do not use System.exit () in the preceding code, do not turn off the CPU, and once the return or throw statement is used in finally, it will invalidate the try block, the return in the catch block, and the throw statement.
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?
- Closing a resource's statement is straightforward and not bloated. It is not necessary to determine if null.
7. Object-oriented design job (group completion, no more than 3 students per group) 7.1 Who is the user of the system?
- Teacher, student, library Manager.
7.2 Main function modules (not too many)
- The loan record, the collection also records, inquires the module.
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, etc.
- Use the Arrylist array to store information.
3. Code cloud and PTA3.1. Codes Cloud Code submission record
3.2 PTA Problem set complete situation diagram
3.3 Count the amount of code completed this week
Week Time |
Total code Amount |
New Code Volume |
total number of files |
number of new files |
5 |
221 |
221 |
4 |
4 |
6 |
487 |
226 |
10 |
6 |
7 |
837 |
350 |
14 |
4 |
8 |
1057 |
220 |
17 |
3 |
9 |
1289 |
232 |
21st |
4 |
10 |
1654 |
365 |
26 |
5 |
11 |
1989 |
335 |
32 |
6 |
201621123041java Programming Tenth Week study summary