[introduction to Java] day16 Java exception handling (Part 1)
Dangdang, Dangdang, ladies and gentlemen, I haven't seen you for a long time. I miss you very much.
Today, let's talk about a goblin in Java, that is exception.
What is an exception? What is exception handling?
Exception, as the name suggests, is abnormal (escape). It is an unexpected thing that happens when a java program runs. It prevents the program from executing normally as expected by the programmer.
Exception handling, it should be said that the exception handling mechanism is a magic weapon specially used to subdue the goblin. The exception handling mechanism in Java allows the program to handle exceptions according to the preset exception handling logic of the code when exceptions occur, so that the program can return to normal and continue to execute as much as possible, and keep the code clear.
In short, Java exception handling allows us to actively confront possible exceptions and deal with them in a mellow way.
Let's take a look at a little chestnut and see what the exception in Java looks like.
Don't panic. Don't collapse when you see the red prompt. Just want to turn off the IDE. Come on, hold my hand and show you the true face of the grinding goblin (funny).
0 is taken as the denominator in the code, so the program will have arithmetic exceptions. After throwing an exception, if there is no processing, the program will be terminated by default, so the subsequent print content is not output. In the exception content, it is explained that the exception type is Java Lang. arithmeticexception, that is, arithmetic exception, followed by the reason for the exception: / by zero, that is, the reason for the exception is that 0 is used as the denominator, and there is stack information behind it, indicating that the exception is thrown at com frank. chapter16. main. Test. Under the main package, line 11 of the test class (if the number of lines is different from what you think, don't care, because there are some indescribable instructions before my code starts), because there is only one method call, there is no long stack information, and it looks very concise and clear.
So you see, in fact, the exception is not so terrible. It not only gives you the reason for the exception, but also tells you what line the bug is in, so making good use of it can help you write more difficult to find bugs. Bah, if you are wrong, it can help you find bugs more easily (manual funny).
What if you don't want the program to end after throwing an exception, but want it to continue running? Then capture it.
How to use exception handling
Let's change the chestnut above:
The output is as follows:
OK, very strong. Now even if an exception is thrown, the program continues to run. Abnormality is like a beast, but once you capture it and tame it, you can use it and do whatever you want.
try... catch... It is a common exception handling collocation. If an exception occurs in the try statement block, if it happens to be caught, it will directly jump to the catch statement block and execute the code in the catch statement, like the chestnut above. Because the exception class is caught, when its subclass exception Java Lang. arithmeticexception can also be captured when it is thrown. The structure and hierarchy of exception class will be described in detail later.
There is another collocation, that is try catch... Finally, the finally statement block is much stronger than catch. As mentioned earlier, the catch statement block must catch a specific exception before executing the code inside. If the catch is an arithmeticexception but throws a null pointer exception, it will not be caught and the exception will run away. At this time, the advantages of finally are shown. No matter what kind of exception is thrown or whether an exception is thrown, the code in finally will be executed. Therefore, the general usage is to release those resources that need to be released in the finally statement block, such as socket connection, closing IO stream, closing database connection, etc. That is to say, generally, you clean up the mess thrown out in the try in finally. It hurts for a second. Finally, as expected, those who can do more work.
Of course, try Finally is also OK. It should be noted that when an exception occurs in the try statement, the code after the exception will not be executed, but will jump to the corresponding catchu or finally.
In the above code, multiple catch statement blocks can be used at the same time. The first catch statement block catches a null pointer exception, but it is not caught because it throws an arithmetic exception, but it is caught by the second catch, so the code in the second catch statement block is executed. Exception matching is performed from top to bottom, and finally the code block in finally is executed. About try catch... Finally, there is another interesting return question. If there is a return in all three statement blocks, what will be the final return result? It is explained in detail here, http://www.cnblogs.com/mfrank/p/7895660.html If you are interested, you can have a look.
In most cases, the code in finally will be executed. In only one case, the code in finally will not be executed, that is, the virtual machine is terminated in the try statement block (for example, use system. Exit (0);).
About exceptions, there is another keyword to be introduced, that is throw. Throw can be used to actively throw an exception. Seeing this, you may be confused and throw it out on your own??? It's not unusual enough. It's not too big to join the fun?? Don't worry, don't worry. There must be some misunderstanding. Put down the knife and have something to say.
The throw keyword is really used to throw exceptions. You can use it as follows:
The output is as follows:
You can throw any type of exception with throw keyword. Of course, you can throw error if you want. As for what is error and the relationship between error and exception, we will explain it in the next article. Don't delve into it for the time being.
When throwing an exception, you can add the reason for throwing the exception, which makes it easier to locate the problem. Of course, it is generally not used like chestnuts. Here is just for simplicity.
So far, the first half of the exception has been explained. In this article, we explain what an exception is, what exception handling is, and how to use the exception handling mechanism. I believe you have a preliminary understanding of this goblin. In the next article, you will explain which members of the exception family, how to use custom exceptions, and the correct posture in the actual use of exception handling. You are welcome to continue to pay attention and plan to update more than two articles a week. If there are omissions or bad explanations, you are welcome to point out in time and make common progress!