Learning experience of Java exception handling

There are always various problems in the program. In order to make the program run normally during the execution process, use the exception handling mechanism provided by java to catch possible exceptions, handle exceptions and make the program run normally. This is java exception handling.

1、 Catcheable exception

Exceptions that can be caught in Java are divided into controllable and runtime exceptions.

1. Controllable abnormality

In Java, those predictable errors can be handled when the program is compiled, and specific error information can be given. These errors are called controllable exceptions. Common controllable exceptions are as follows:

Exception description IOException when an I / O exception occurs, this exception is thrown. Sqlexception provides exceptions about database access errors or other error information. The classnotfoundexception class does not find the exception. The signal generated when the nosuchfieldexception class does not contain a field with the specified name. When nosuchmethodexception cannot find a specific method, this exception is thrown

2. Abnormal operation

In Java, errors that cannot be detected by the compiler are called runtime exceptions. Common runtime exceptions are as follows:

Exception description indexoutofboundsexception indicates that the index value of a collection or array is out of range. NullPointerException is thrown. When the application attempts to use null where an object is needed, the exception arithmetexception is thrown. When the operation condition of the exception occurs, Throw this exception illegalargumentexceptionthrowing an exception indicates that an illegal or incorrect parameter ClassCastException was passed to the method. This exception is thrown when trying to cast an object to a subclass that is not an instance

2、 Handling exceptions

In Java, when an exception occurs in a program, you can use try ・ catch, try ・ catch ・ finally or try ・ finally to handle it.

1. Use try ・ catch to handle exceptions

Try is followed by a normally executed statement, while catch is followed by an exception handling statement. The parentheses of catch are the types of exceptions that the program needs to handle. The syntax format is as follows:

Here is an example of arithmetic anomaly, as follows.

Here 1 / 0 is an abnormal algorithm because the divisor cannot be 0. The operation results are as follows:

Because there are exceptions, the statement after try is not executed, so the statement after catch is executed. Where "e.getmessage()" is the method to obtain exception information, which is used to obtain detailed message strings; In addition, there is the printstacktrace () method, which is used to output its stack trace to the standard error stream; The toString () method is used to get a short description.

2. Use try ・ catch ・ finally to handle exceptions

Here, the statements after try and catch are the same as those before, and the statements after finally must be executed regardless of whether an exception occurs. Therefore, the finally statement block is usually used to perform garbage collection. The syntax format is as follows:

3. Use try ・ finally to handle exceptions

When an exception occurs in the program, it can be processed in the finally statement block. In addition, when no exception occurs in the program, the code in the finally statement block will also be executed after executing the statement between try and finally. The syntax format is as follows:

3、 Throw exception

For exceptions in the program, in addition to the above try ・ catch statement, you can also use the throws declaration or the throws statement to throw exceptions.

1. Throw an exception using the throws declaration

Throws is used for method declaration. When declaring a method, the throws declaration is used to throw an exception, and then the exception is handled in calling the method.

If multiple exceptions need to be declared, they should be separated by commas. The syntax format is as follows:

For example, throw an exception with throws.

2. Throw an exception with the throw statement

If you want the program to throw an exception by itself, you can use the throw statement. The syntax format is as follows: throw new exception ("description of exception");

What is thrown with the throw statement is an instance of the exception class, which is usually used with the if statement. For example:

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
分享
二维码
< <上一篇
下一篇>>