"Crazy Java Lectures" 10

1.  Java's exception mechanism mainly relies on five keywords: try, catch, finally, throw and throws.    

2. Java exceptions are divided into two types, Checked exceptions and Runtime exceptions. Java believes that Checked exceptions are exceptions that can be handled at compile time, so it forces Java to handle all Checked exceptions, while Runtime exceptions do not need to be handled.

3 .  Use try ... catch to catch exceptions:

    The grammatical structure is as follows:

image

 

4.  If an abnormal situation occurs during the execution of the code in the try block, the system will automatically generate an exception object, which is submitted to the Java runtime environment. This process is called throwing an exception. When the Java runtime environment receives this exception, it will look for the Catch block that handles the object. If a suitable catch block is found, the object will be handed over to the catch for processing. This process is called catching the exception. Of course, if there is no Find a suitable catch block, then the program will terminate.

5. When the  program executes the code in the try, if it encounters an exception, it will generate an Exception. As mentioned above, the appropriate catch will be called for processing. What I want to explain here is that every exception that can be received has its own corresponding catch statement, which is unique.

6.  Java divides all abnormal situations into two types, one is Exception and the other is Error. Error errors generally refer to problems related to virtual machines, such as system crashes, virtual machine errors, and dynamic link failures. This kind of error cannot be recovered or caught, and will cause the application to be interrupted, so the application should not try to use the catch block to catch the Error object.

7. The difference between try statement and if statement:

    The curly braces after try and catch statements cannot be omitted, but if there is only one line after the if statement, the curly braces can be omitted.

8. Look at a chestnut that catches exceptions:   

image

The wording is Jiangzi. The red box shows the usage of try and catch, and the purple box shows the usage of Scanner we talked about before.

Let me show you the results:

Example 1:

image

Example 2:

image

Change another input method:

image

Let's try to catch different exceptions:

Example 3:

image

If you replace the data in brackets with:

image

The result is (example 4):

image

    The above-mentioned exception handling is more common, and familiarity is best.

 

9. When  catching exceptions, pay attention to catch small exceptions first, and then catch big exceptions. In other words, put the larger ones at the back of the program, and put the more specific ones at the front. Using the above example, it will be

image

    The content in the red box is put later, because this contains a lot of situations, and the above are more specific, so the first is small and then the big. Another example: runtime exceptions should be placed after the null pointer exception.

 

10. Multiple exception capture provided by Java7

    Before Java7, each catch can only catch one kind of exception, but after Java7, a catch block can catch multiple exceptions.

Let's look at an example first:

image

The above is the same as the previous code, and the result is as follows:

image

This is multi-exception capture. Two points to note are:

(1) Writing problem: multiple exceptions are separated by "|":

image

(2) When capturing multiple types of exceptions, the exception variables are implicitly finalized, so the program variables cannot be reassigned to the exception variables.

Give a chestnut:

image

Look at the compilation information:

image

But if only one type of abnormal variable is captured, it can be reassigned:

image

There is no problem with the compilation.

 

11. Access exception information

(You can ignore a bunch of words in the following Ula Ula, eat chestnuts)

    If the program needs to access the relevant information of the exception object in the catch block, it can be obtained by accessing the post exception formal parameter of the catch block. When the Java runtime decides to call a catch block to handle the exception object, it will assign the exception object to the exception parameter after the catch block, and the program can obtain information about the exception through the parameter.

Come on~ Chestnut:

 

image

The results are as follows:

image

    All exception objects contain the following commonly used methods:

    1) getMessage(): returns a detailed description string of the exception;

    2) printStackTrace(): Output the trace stack information of the exception to the standard error output.

    3) printStackTrace (PrintStream s): output the abnormal trace stack information to the specified output stream;

    4) getStackTrace(): Returns the trace stack information of the exception.

 

12. Use finally to recycle resources

(The words below can only be seen in bold)

    If the program opens physical resources (such as database connections, network connections and disk files, etc.) in the try, these physical resources must be displayed for recycling. When we talked about the garbage collection mechanism before, we said that the system can only automatically reclaim the memory occupied by some objects in the memory, and cannot reclaim physical resources. So what to do? If the recovery process is performed in try or catch, the program may not be executed, so we cannot put it in these two places. To solve this problem, the exception handling mechanism provides a finally block, which will execute finally regardless of whether an exception occurs. Multiple catch blocks are placed after try, and finally blocks are placed after catch blocks.

Come on~ Chestnut:

 

image

The results are as follows:

image

 Come again~ Chestnuts:

 

image

Make a slight change in the above try, as shown in the figure above. The result is unchanged. What does this show? The return in our impression is quite powerful. The general method will end when the return is executed, but it does not end here. Finally, it will continue to execute. Pay attention here. But it's not that we have no choice but to finally.

Here again~ Chestnut:

 

image

The meaning of this statement is to exit the virtual machine, so the program will not execute the finally block, the result is as follows:

image

13.  Above we said that the finally block can be used to reclaim physical resources, but we can also see that the amount of code will increase a lot. After Java7, a seemingly simpler method is provided. Look at the example:

image

But, to be honest, I don’t like this writing method, it may be used in the future, but,,, I will talk about it later (Ao Jiao face.jpg || 傲娇脸.gif)

 

14. Use the throws statement to throw an exception

    The idea of ​​using the throws statement to throw an exception is: the current method does not know how to handle the exception, and it is handed over to the upper level for processing. If the main method does not know how to handle it, you can also use the throws statement to throw an exception and hand it to the virtual machine for processing. The way the virtual machine handles is to print the abnormal stack trace information and terminate the program operation (this is the example we gave above).

    The syntax format of throws statement to throw an exception is:

    throws ExceptionClass1,ExceptionClass2...

If you use throws, you don't have to try...catch.

Give a chestnut:

 

image

The result of this program is the same as the result of our previous use of try...catch.

 

END

 

image

  

 

Guess you like

Origin blog.csdn.net/allein_STR/article/details/113986747