Exception handling mechanism of Java core foundation

In the development process, various exceptions are always encountered, such as the most common null pointer exceptions and array out-of-bounds exceptions. These exceptions mean that when the method cannot be completed in the normal way, the method can be exited by throwing an exception. The error information and reason during the execution of the method are encapsulated in the exception. The caller can obtain the exception according to the business The situation chooses to handle the exception or continue to throw the exception. Exceptions directly affect the robustness of the program and the stability of the system, and the quality of the exception handling mechanism also represents the robustness of a computer language. As a language with excellent robustness, Java naturally has Complete exception mechanism, and as a developer, it is important to understand the comprehensive exception handling mechanism and handle exceptions well.

In Java, Throwable is the parent class of all errors or exceptions. Throwable can be divided into Error and Exception. Common Errors include AWTError, ThreadDeath, and Exception can be divided into RuntimeException and CheckedException. The following is the structure diagram of the exception handling class in Java:

 The Error class refers to internal errors and resource exhaustion errors of the java runtime system. The application will not throw such objects. If such an error occurs, in addition to telling the user, all that is left is to try to make the program terminate safely. In general, we will not handle this kind of error in the program.

RuntimeException: Refers to the exception thrown during the normal operation of the Java virtual machine. RuntimeException can be caught and processed. If a RuntimeException occurs, it must be caused by a program error. We usually need to throw the exception or catch and handle the exception. Common RuntimeExceptions include NullPointerException, ClassCastException, etc.

CheckedException: During the compilation phase, the Java compiler will check the CheckedException and force the program to catch and handle such exceptions. That is, the program is required to catch and handle the exceptions through the try catch statement block where exceptions may occur. Common CheckedExceptions include IOException, SQLException, ClassNotFoundException, etc. caused by I/O errors. This type of exception is generally caused by opening the wrong file, SQL syntax error, class non-existence, etc.

Generally, we need to deal with Exception. There are two ways to handle exceptions: throwing exceptions and using try catch block to catch and handle exceptions.
Throwing an exception: When encountering an exception, no specific treatment is performed, but the exception is thrown to the caller, and the caller will handle it according to the situation. It may be directly captured and processed, or it may continue to throw exceptions to the upper layer. There are three forms of throwing exceptions: throws, throw, and the system automatically throws exceptions. Among them, throws function on the method, used to define the exception that the method may throw; throw function in the method, which means that an exception is explicitly thrown. The specific usage method is as follows:

public static void main(String[] args) { 
    String s = "abc"; 
    if(s.equals("abc")) { 
        throw new NumberFormatException(); 
    } else { 
        System.out.println(s); 
    } 
} 
public int div(int a,int b) throws Exception{
    return a/b;
}

Throws acts on the method, followed by multiple possible exceptions, used to declare the exceptions that may occur during the operation of the method, so that the caller can predefine different processing methods according to different exception types; throw acts inside the method , Followed by the specific exception object, used to throw the object encapsulating the exception information, the subsequent code will not be executed when the program is executed to throw, but jump to the caller, and throw the exception information to the caller By. In other words, the statement block after throw will not be executed, except for the finally statement block.

In addition to throwing exceptions, we can also use try catch statements to handle exceptions. For example, in the following method, if a string is converted to an integer exception, it returns a default value of -1. The code is as follows:

private int parseStrToInt(String numberStr){
    int number = 0;
    try{
        number = Integer.parseInt(numberStr);
    }catch (NumberFormatException e) {
        number = -1;
    }
    return number;
}

 

 

Guess you like

Origin blog.csdn.net/wk19920726/article/details/109240520