The exception handling mechanism of Java can help us avoid or handle possible errors of the program, so that the program will not terminate unexpectedly when it encounters some recoverable errors, but will handle these errors, it also makes it unnecessary for us to write a large amount of code to check errors when writing a program, enhancing the readability and logic of the Code. In Java, an exception represents an incorrect object. Exceptions can be classified into two types. One is serious errors, such as hardware errors and insufficient memory. They correspond to the Error classes and their subclasses under the java. lang package. Normally, such error programs cannot be recovered and the execution of the program needs to be interrupted. The other type is non-serious errors, such as illegal data input and division by 0, which correspond to java. the Exception class in the lang Package and its subclass. This error can be recovered without affecting the running of the program. We can use the try, catch, and finally keywords to catch exceptions. 1. try and catch put the statements that may cause exceptions in the try {} block, and then capture them in the catch {} block. For example, if an exception is obtained by dividing 0: [java] public class SimpleDemo {// division public static int devision (int a, int B) {return a/B ;} public static void main (String [] args) {try {// 5 divided by 0 SimpleDemo. devision (5, 0); System. out. println ("Exception");} catch (Exception e) {e. printStackTrace ();} System. out. println ("Finish") ;}} execution result: we can see that the program has not been terminated due to a zero-division error. We also found that the System. out. println statement under SimpleDemo. devision () is not executed. Once an exception occurs, the program jumps out of the current execution position without executing the statement after the exception. 2. The statements in the finallyfinally statement block are executed no matter whether the exception occurs or not. Some may ask, since the statements in the finally block will be executed no matter whether an exception occurs or not, what is the actual effect of finally? I don't need to use finally to write it out, can I? In the above example, we add a return: [java] public class SimpleDemo {// division Operation public static int division (int a, int B) to the catch statement block) {return a/B;} public static void main (String [] args) {try {// 5 divided by 0 SimpleDemo. division (5, 0); System. out. println ("Exception");} catch (Exception e) {e. printStackTrace (); return; // return of the main function} finally {System. out. println ("Finally");} System. out. println ("Finish") ;}} at this time, the finally outer Finish The finally block is not printed, but the Finally block is printed. Finally is very useful in actual 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 resource release code in the finally block. However, it should be noted that finally blocks will not be executed in one case. If the program exits before the finally block is executed, for example, if the System. exit () method is called, The finally block cannot be executed. 3. Throwing an exception if an exception occurs in a method, but we do not want to directly handle this exception in the method, but want the method caller to handle it, you can use the throws keyword to declare this method to throw an exception. This is very common in the API functions provided by Sun, such as java. io. the read method in Reader is declared as throwing an IOException: public int read (char [] cbuf) throws IOException at this time, when calling the read method, we must put it in the try statement block for exception capture. Otherwise, the compiler will report an error and force us to capture exceptions. Of course, if we do not want to handle exceptions when calling read, we can also declare the method for calling the read method as throws IOException, so that the exception will be thrown again. If an Exception is declared in the main function, the Exception information will be captured and processed by JVM. The result of JVM processing is to print the Exception information and terminate the program. 4. Exception Handling framework 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 declare an Exception object in catch to capture all Exception classes and their subclasses. But pay attention to the catch writing sequence. If there are multiple catch objects after a try and the Exception object declared in the first catch object, the Exception will be directly processed by the first catch object, the exception cannot be caught in the catch later. This type of error occurs during compilation. For example: [java] public class CatchDemo {// division Operation public static int division (int a, int B) {return a/B ;} public static void main (String [] args) {try {CatchDemo. division (4, 0);} catch (Exception e) {System. out. println ("Exception Class");} catch (ArithmeticException e) {System. out. println ("ArithmeticException Class") ;}} the output ArithmeticException of the compiler has been captured, meaning that the Exception above has already caught this Exception and does not need to be captured repeatedly. What will happen if these two catch statements are reversed? [Java] public class CatchDemo {// division Operation public static int division (int a, int B) {return a/B;} public static void main (String [] args) {try {CatchDemo. division (4, 0);} catch (ArithmeticException e) {System. out. println ("ArithmeticException Class");} catch (Exception e) {System. out. println ("Exception Class") ;}} at this time, we found that the code was compiled, and the execution result was that ArithmeticException caught this Exception, while the catch later was not captured.