1. This week study summary 1.1 summarizes the set of related content in the way you like (mind map or other).
- Keywords:
try...catch...finally
, try...catch...
,try...finally...
- The difference between error and exception:
| -Error: Refers to JVM errors, error before program execution, user cannot manipulate processing
| -Exception: Refers to error during program execution, user can handle
So so-called exception handling refers to the handling of all exception and its subclass exceptions.
The difference between throws and throw:
| -throw: Refers to a method in which an exception class object is thrown, and the exception can be defined by itself or already exists.
| -throws: A declaration for a method that indicates that this method must handle exception handling when it is called, and that if there is an exception, the exception is handled normally without exception.
2. Written workThis job title set异常
2.1. Common exceptions7-1 answer with topic set
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?Frequently occurring exceptions are:
- Null Pointing exception: NullPointerException
- Class Conversion Exception: ClassCastException
- Array out-of-bounds exception: ArrayIndexOutOfBoundsException
Format exception: NumberFormatException
A: These exceptions do not need to be captured because they all inherit from the RuntimeException
exception that belongs to the Unchecked Exception
class.
Avoidance methods:
- When invoking the
String
equals () method of a class, the string to be compared can be entered before, eg:"!!!".equals(str);
Pay more attention to the array and the formatted expression while entering the operation
2.1.2 What kind of exception is required for users to be sure to use capture processing?
checked Exception
exceptions that belong to classes must use capture processing.
- The
throws
method declared with the keyword needs to be used for capture processing at call time.
Capture is throw
required for exceptions that are out of their own
2.2. Handle exceptions to make your program more robustTopic 7-2
2.2.1 Experimental summary. And answer: How can you make your program more robust?
- Exception trapping is handled in the field where the program may have an exception,
try...catch
so that after the exception is processed, the program does not break down because of an exception.
- In this question, because the input may be a character instead of a number, it is used
sc.next()
as an input.
- The next input is entered without an error in the input, otherwise it is re-entered, so it is placed in the try statement after the input operation in the For Loop
i++
.
Exceptions are handled by statements that may appear to be wrong data[i] = Integer.parseInt(sc.next())
.
2.3. Throw and throwsTopic 7-3
Read the Integer.parsetInt
source code
2.3.1
Integer.parsetInt
What is the benefit of having a large number of code throwing exceptions at the outset?
- Benefit: Throws
NumberFormatException
an exception that allows the user to know the cause of the exception.
The cause of the exception occurred:
1. String s = null
the input string is empty
2. The input is radix
less than Character.MIN_RADIX
or greater thanCharacter.MAX_RADIX
3. fristChar
not a number or number +
-
.
4. The input S has only one +
or -
no other digital content
5. Exceeding the Extremum
2.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?
Need to tell the caller that the type of the exception and the ' message ' of the exception occurred
2.4. Improve Arrayintegerstack with exceptionTopic 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?
- Advantages: The use of throwing exceptions to represent the program runtime error, the program in the run error will be thrown directly exception, so that the program is aborted, it is easy for us to find the exception, handling exceptions.
If you use a method that returns an error value, you will not be able to know exactly where the program was and what error occurred.
2.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?
- Don't necessarily use
throws
keywords
- If you use the
throws
keyword to declare the exception thrown by the method, then it is tantamount to throwing the exception to the used to handle it, and you do not have to handle the exception in the method.
The advantage: The exception that is generated in the method is all given to the used processing, so that the code will be more clear, there will be no way to deal with their own anomalies in the case.
2.5. Function questions-capture of multiple exceptionsTopic 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?
It is important to note that the use of the keyword catch()
, the parent exception to be caught after the subclass exception, or a compilation error will occur.
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?
The catch of a subclass exception must be placed before any of its parent class exceptions are caught, and Exception e
the catch of the exception is placed last because it is the parent of all exceptions.
2.6 Add exception handling for 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));//打印数组内容
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.
Operation Result:
2.6.2 Combination Problem Set
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?
- To place the action that frees the resource in a
finally
block, because the finally
statement in is executed regardless of whether the program has an exception
Note: The action to release the resource sometimes also has an exception, so it is necessary to finally
add an try - catch
exception capture processing action in the block.
2.6.3 uses Java7 try-with-resources
to rewrite the above code to automatically close the resource. What are the benefits of this approach?
Benefits:
1. Less finally
block of statements, the program is simplified without editingfinally
2. The program automatically calls close()
the method to release the resource, so the resource will eventually be freed, regardless of whether the try statement is functioning properly.
2.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.
2.7.1 who is the user of this system?
Students in school
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.
member |
responsible for module |
Lu Weibin |
Borrowing books, viewing borrowed books |
Yu Yuming |
View personal information, browse books |
Su Zhiying |
Return book, Time lapse |
2.7.3 the main class design and class diagram of the system (available)
2.7.4 how you plan to store library information, address information, and reader information.
- Store information: add element in dynamic array by adding () method
- Resolution Information: The Contains () method finds the information to be processed in the total information and then accesses the element
Reader's information: ArrayList
3. Code Cloud and PTATopic Set:jmu-Java-05-集合
3.1 Yards cloud Code 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 |
120 |
120 |
2 |
2 |
2 |
280 |
160 |
5 |
3 |
3 |
590 |
310 |
10 |
5 |
4 |
780 |
190 |
12 |
2 |
5 |
1290 |
510 |
17 |
5 |
6 |
1710 |
420 |
20 |
3 |
7 |
2513 |
803 |
24 |
4 |
8 |
2513 |
0 |
24 |
0 |
9 |
3014 |
501 |
27 |
3 |
10 |
3441 |
327 |
30 |
3 |
11 |
4121 |
680 |
37 |
7 |
Java Week 11th job