Analysis of Java exception handling examples

This article describes the usage of Java exception handling. Share with you for your reference. The specific analysis is as follows:

Java's exception handling mechanism can help us avoid or deal with possible errors in the program, so that the program will not terminate unexpectedly when encountering some recoverable errors, but deal with these errors. It also makes us not have to write a lot of code to check the error situation when writing the program, which enhances the readability and logic of the code. In Java, an exception represents an incorrect entity object.

Anomalies can be divided into two categories; One is serious errors, such as hardware errors, insufficient memory, etc., which correspond to Java Error class and its subclasses under Lang package. Usually, this kind of error program itself cannot be recovered, and the execution of the program needs to be interrupted; The other is non serious errors, such as illegal data entered by the user and divided by 0. They correspond to Java The exception class and its subclasses in Lang package can generally be recovered without affecting the operation of the program. We can use try, catch and finally keywords to catch exceptions.

1、try,catch

Put the statements with possible exceptions into the try {} block, and then catch them in the catch {} statement block. Exception if 0:

Execution result:

You can see that finish is printed, indicating that the program did not terminate because of an error divided by 0. At the same time, we also found that the exception simpledemo System. Under deviation() out. The println statement was not executed. Once an exception occurs, the program will jump out of the current execution position without executing the statement after the exception.

2、finally

The statements in the finally statement block will be executed whether an exception occurs or not. Some people may ask, since the statements in the finally block will be executed regardless of whether an exception occurs or not, what is the practical role of finally? I don't have to finally write it out, okay? As in the above example, we add a return to the catch statement block:

At this time, the finish outside the finally block is not printed, while the finish inside the finally block is printed.

Finally, it is very useful in practical development. For example, if we open a database and an exception occurs when the database reads and writes data, we should close the database connection and release the corresponding resources. At this time, it is most appropriate to write the code for releasing resources in the finally block.

Note, however, that finally blocks will not be executed in one case. If the program exits before executing to the finally block, such as calling system Exit () method, the finally block will not be executed.

3. Throw out exception

If an exception occurs in a method, but we don't want to handle the exception directly in the method, but want the caller of the method to handle it, we can use the throws keyword to declare the method to throw the exception. This is very common in the API functions provided by sun, such as Java io. The read method in the reader is declared to throw an IOException:

At this time, when we call the read method, we must put it in the try statement block to catch exceptions, otherwise the compiler will report an error and force us to catch exceptions. Of course, if we really don't want to handle exceptions when calling read, we can also declare the method calling read method as throws IOException, so that the exception will be thrown again. If we declare that an exception is thrown in the main function, the exception information will eventually be captured and processed by the JVM. The processing result of the JVM is to print the exception information and then terminate the program.

4. Framework of exception handling

All exception classes are derived from the exception class. This means that if we are not sure what type of exception will occur, we can directly declare an exception object in catch and catch all exceptions of exception classes and their subclasses. But pay attention to the writing order of catch. If there are multiple catches after a try and the exception object is declared in the first catch, the exception will be directly processed by the first catch, and the following catches cannot catch the exception. This error will generate an error when compiling. Examples are as follows:

The compiler output arithmeticexception has been caught, which means that the above exception has caught this exception and there is no need to catch it again.

What happens if these two catches are reversed?

At this time, we find that the code has passed the compilation, and the result of execution is that the arithmeticexception caught this exception, but the following catch did not catch it.

I hope this article will be helpful to your Java programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>