1. Summary of this week's study
1.1 Summarize abnormal content 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?
For:
ArrayIndexOutOfBoundsException
Illegal index access Array
ClassCastException
Type cast exception
NullPointerException
Null pointer exception, the object being called is null
NumberFormatException
Numeric to format exception
IllegalArgumentException
Passing Illegal parameter exceptions
IndexOutOfBoundsException
Subscript out-of-bounds exception
ClassCastException NullPointerException NumberFormatException IllegalArgumentException IndexOutOfBoundsException
Do not need to capture, these exceptions are runtimeexception exceptions, this is the logic of the program design problems, if you want to avoid these exceptions also need to write their own code when the idea and logic correct, syntax specification.
1.2 What kind of exception requires the user to be sure to use capture processing?
A: In the exception of exception and its subclasses must be captured, RuntimeException and its subclasses do not need to capture
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?
A: This problem is mainly when the input is not a number, the exception is generated, and in the case of anomalies need to do i--operation.
Method: When using the Integer.parselent () method, you need to use the try-catch mechanism to catch the exception to make the program more robust.
3. Throw and throws
Topic 7-3
Read the Integer.parsetInt
source code
3.1 Integer.parsetInt
There was a lot of code thrown out of the exception at the outset, what's the benefit of this practice?
Benefit: The first throw is the numberformateexception exception, you can know the cause of the exception in the event of an 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: When throwing an exception, you need to pass it to the caller for the reason of the error and different error causes. For example, 7-3 throws an exception for the reason that the begin is no less than end,begin less than 0, and end is greater than arr.length.
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?
A: The method of throwing an exception can not only display the type of error, but also implement the cause of the error, so that the user can know where the error is and how to modify it.
For example, the Peek method in 6-3, if(this.empty()) throw new EmptyStackException();
if empty, throws a Emptystackexception exception, so it is convenient to know the stack empty stack full.
4.2 If the inner code of a method throws an exception of type RuntimeException, is the method declaration supposed to use the keyword throws
, and if the throws
exception that is thrown by the method is declared with the keyword, what is the benefit to us?
A: You do not need to use the Throw keyword to declare, because RuntimeException belongs to unchecked Exception and is automatically detected when the program is run.
Use the Throw keyword to declare that exceptions can be thrown to other classes or methods when they do not want to use try-catch capture.
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?
A: a try block may throw multiple exceptions, while catch captures need to be aware of the order in which they are caught, the subclass exception is to precede the parent exception, otherwise the subclass exception is not captured, in topic 6-1 catch (NumberFormatException e) and catch ( IllegalArgumentException e) Put in front, catch (Exception e) should be placed at the end.
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?
A: You need to be aware that when catching a catch, the exception subclass is before the parent class, otherwise the child class is not caught
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 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.
For:
6.2 6-2
What do you want to put in the finally block in conjunction with the title set code? Why? What do I need to be aware of when closing resources using finally?
A: To put the statement that must be executed in the finally block, because finally whether or not the exception will be executed, in the use of the Finally statement also use Try-catch capture, because there is also a possible exception in the finally block.
6.3 Use Java7 try-with-resources
to overwrite the above code to automatically close the resource. What are the benefits of this approach?
Benefit: Without the use of the finally block, the code is streamlined and the system shuts down automatically without the need to consider resource shutdowns.
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.
Group:
Huang 201621123045
Weng Huahui 201621123042
7.1 Who is the user of this system?
A: Students and administrators.
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
Students:
Student Registration
Student Login
View Books
Borrowing books
Return books
Administrator:
View Books
Add a book
Delete a book
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.
For:
Store the information of the book using the map key value pair, the key value to put the book, value values put the number of books.
The borrowed information is also stored using the Map key value pair
The reader's information is stored using the Set collection
8. Select: Use exceptions to improve your shopping cart system
Give 1 examples of how you can use the exception handling mechanism to make your program more robust.
The description consists of 2 parts: 1. The problem description (where the exception is encountered). 2. Solution (Key Code)
9. Select: Use the static Code scanning Tool
Scan your own shopping cart code
8.1 Analyze the problems in your own code that arise in terms of specifications.
8.2 Think about how to solve these problems.
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 |
2 |
2 |
|
|
2
213 |
213 |
Ten |
5 |
3 |
417 |
204 |
|
5 |
5 |
676 |
259 |
+ |
7 |
6 |
1071 |
405 |
7 |
|
7 |
1780 |
709 |
|
6 |
8 |
1900 |
|
|
5 |
9 |
2335 |
435 |
|
6 |
|
2767 |
432 |
7 |
|
one |
3321 |
595 |
|
9 |
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
Optional: 5. Use Java to solve practical problems (attendance Auto-statistics)
Association check-in, each time using a paper signature table to check in, go back and then manually statistics. If you have more than one signature table, manual statistics are required. Now keep the paper signature method unchanged, responsible for each time the paper signature table will be manually entered after the system, so that the data can be automatically counted. What functional modules are required to implement such a system? Trying to encode the implementation?
201621123042 Java Programming Tenth operations