1. Summary of this week's study
- Because the finally clause is always executed, it is possible to return from multiple points in a single method, and to ensure that important cleanup work is still performed.
- When the constructor is invoked, cleanup of objects is generally used
finally
. However, it is important to note that if the constructor fails and throws an exception, finally
it cannot be cleaned. To add one more layer of try-catch for shutdown processing. Similarly, for those constructors that can fail InputFlie
, let's add a layer of try-catch accordingly.
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?
Frequently appearing are:
NullPointerException
--NULL pointer exception
ClassCastException
--Type conversion exception
IndexOutOfBoundsException
--Array out of bounds exception
There is no need to capture, because these exceptions belong to unchecked
exceptions.
To avoid these anomalies, we need to deal with special situations where exceptions occur. It is typical to use an if statement for type and array judgments.
1.2 What kind of exception requires the user to be sure to use capture processing?
Checked Exception
Requires that the user be sure to capture the processing. Use the Try-catch or throws keyword to declare an exception that might be thrown
2. Handle exceptions to make your program more robust 2.1 experimental summary. And answer: How can you make your program more robust?
- Experimental summary: In this problem, we can avoid empty elements in the array by putting i++ in the last sentence of the try clause, thus bypassing the error element and filling up the array.
- A: Reasonable application of exception handling mechanism to make the program more robust. For example, in this problem, we can catch an element that does not conform to the required type as an exception through the exception handling mechanism.
3. Throw and throws3.1
Integer.parsetInt
What is the benefit of having a large number of code throwing exceptions at the outset?
201621123086 Java programming 10th Week of study summary