There are always various problems in the program. In order to enable normal operation during the execution of the program, the exception handling mechanism provided by Java is used to catch possible exceptions, handle exceptions, and enable the program to run normally. This is Java exception handling.
1. Catchable exceptions
Exceptions that can be caught in Java are divided into controllable and runtime exceptions.
1. Controllable exceptions
In Java, those predictable errors can be processed during program compilation and specific error information can be given. These errors are called controllable exceptions. Commonly used controllable exceptions are as follows:
Exception Description IOException When some I/O exception occurs, this exception is thrown. SQLException provides information about database access errors or other errors. ClassNotFoundException. The exception is not found. NoSuchFieldException. The signal generated when the class does not contain a field with the specified name. NoSuchMethodException. The exception is thrown.
2. Runtime exception
Errors that cannot be detected by the compiler in Java are called runtime exceptions. Common runtime exceptions are as follows:
Exception Description IndexOutOfBoundsException Indicates that the exception is thrown when the index value of a collection or array is out of range NullPointerException throws the exception when the application tries to use null where the object is needed ArithmeticException throws this exception when an exception operation condition occurs IllegalArgumentException throws an exception indicating that an illegal or incorrect parameter is passed to the method ClassCastException throws the exception when trying to cast the object to a subclass that is not an instance
2. Handle exceptions
When an exception occurs in Java, you can use try... catch, try... catch... finally or try... finally to handle it.
1. Use try....catch to handle exceptions
After try, there are statements that execute normally, and after catch, there are statements that handle exceptions. The brackets of catch are the exception types that the program needs to handle. The syntax format is as follows:
try { statements that execute normally} catch(Exception e) { statements that handle exceptions}Here is an example of arithmetic exception, as follows.
public class ExceptionTest { public static void main(String[] args) { int result = 1 / 0; try { System.out.println(result); } catch (Exception e) { System.out.println("Throw exception: " + e.getMessage()); } } }Here 1/0 is an exception algorithm because the divisor cannot be 0. The operation results are as follows:
Because there is an exception, the statement after try is not executed, so the statement after catch is executed. Among them, "e.getMessage()" is a method to obtain exception information, which is used to obtain detailed message strings; in addition, there is also the printStackTrace() method, which is used to output its stack trace to a standard error stream; and the toString() method is used to obtain a short description.
2. Use try. catch. finally handle exceptions
Here, the statements after try and catch are the same as those before, and the statements after finally have to 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:
try { statements that execute normally} catch(Exception e) { statements that handle exceptions} finally { statements that will definitely be processed}3. Use try. Finally handle exceptions
When an exception occurs in the program, it can be processed accordingly in the finally statement block. In addition, when there is no exception in the program, after executing the statement between try and finally, the code in the finally statement block will be executed. The syntax format is as follows:
try { statements that need to be executed} finally { statements that will definitely be processed}3. Throw an exception
For exceptions that occur in the program, in addition to the above try... catch statement processing, you can also use throws declaration or throws statement to throw exceptions.
1. Use throws declaration to throw exception
throws is used for method declaration, throws an exception when declaring a method using throws declaration, and then handles the exception in calling the method.
If you need to declare multiple exceptions, each exception should be separated by commas, and the syntax format is as follows:
Data type method name (formal parameter list) throws Exception class 1, Exception class 2, ..., Exception class n { method body;}For example, throw an Exception exception using throws.
public void showInfo() throws Exception { // Throw Exception FileInputStream in = new FileInputStream("C://Record.txt"); // Create IO object}2. Use throw statement to throw exception
If you want the program to throw an exception on its own, you can use the throw statement to achieve it. The syntax format is as follows: throw new Exception("Exception");
What is thrown using the throw statement is an instance of the exception class, usually used with the if statement. like:
if(x < 0) { throw new Exception("Program exception, x cannot be less than 0.");}