Seventh: " exceptions, assertions and logs " Summary of Knowledge points
this week, we learned about exception handling in Java, and here are some of the gains.
I. The difference between an unchecked exception and a checked exception:
unchecked Exception handling method: 1, capture 2, continue to throw 3, do not handle.
checked Exception handling methods: 1, continue to throw, negative methods, can always be thrown into the Java virtual machine to handle
2. Capture with Try...catch ( note that the exception to check must be handled, or must be caught or must be thrown.) )
The Try-catch statement can also include the third part, which is the finally clause. It represents what should be done regardless of whether an exception occurs. The last step in catching an exception is to provide a unified exit for exception handling through the finally statement, which allows the control process to manage the state of the program uniformly before it goes to the rest of the program.
Second: The handling of exceptions
An active approach; know exactly how to deal with exceptions that should be captured;
Negative handling methods: Exception declarations that do not know how to handle the throw.
if Try-catch is an active way of dealing with unusual events, then throws is a negative approach.
(1) a try block can be paired with multiple catch chunks, but the catch arrangement has a certain rule, which is related to the exception class within the catch parenthesis. If the exception class in each catch has an inheritance relationship, the subclass is to be ranked before, the parent class is queued, and if there is no inheritance, it is arranged freely.
(2) If there is a program code regardless of whether or not an exception occurs, you want to be executed, you can put code in the finally block, but the finally block can not exist alone, must be paired with Try-catch use
Three: Assertions
the assertion (Assert) syntax is as follows: 1, assert condition or 2, assert condition: both forms of the expression will judge the Boolean "condition", if the result is False (false), the program is already in an incorrect state , the system throws a assertionerror, gives a warning, and exits. In the second form, the expression is passed into the Assertionerror constructor and is converted to a message string. When the program is running, the assertion statement in the program will have no effect if the assertion function is turned off. If the assertion function is turned on, the value of the assertion condition is evaluated, and if its value is false, the statement strongly throws a Assertionerror object. The Open method has been mastered in the Experiment Class (-EA),
Java exception handling mechanism, so that the program better maintenance, if mastered this part of knowledge, skilled use, will undoubtedly become our compiler helper.
201671010140.2016-2017-2 Java program Nineth Week of Java Learning