[Java] Java exception handling mechanism

Scoring points: exception handling, exception throwing, exception tracking stack

standard answer:
the exception handling mechanism can make the program have excellent fault tolerance and robustness . When the program runs in an unexpected situation, the system will generate an Exception object To notify the program, so as to realize the separation of "the code of the business function realization part" and "the code of the error handling part", so that the program can obtain better readability.
Java's exception mechanism can be divided into three parts : exception handling, exception throwing and exception tracking stack issues .
The statement for handling exceptions consists of three parts: try, catch, and finally. The try block is used to wrap business code, the catch block is used to catch and handle a certain type of exception, and the finally block is used to recycle resources. If an exception occurs in the business code, the system will create an exception object and submit the exception object to the JVM, and then the JVM will find a catch block that can handle the exception, and hand the exception object to the catch block for processing. If the JVM does not find a catch code block that can handle the exception, the runtime environment will terminate and the Java program will exit. If the business code opens a certain resource, it can be closed in the finally block, because the finally block will definitely be executed regardless of whether an exception occurs (generally).
When an error occurs in the program, the system will automatically throw an exception. In addition, Java also allows programs to actively throw exceptions. In the business code, when the condition for judging an error is true, you can use the throw keyword to throw an exception. In this case, if the current method does not know how to handle the exception, you can declare an exception to be thrown through the throws keyword on the method signature, and the exception will be handed over to the JVM for processing.
When a program is running, a series of method calls often occur, forming a method call stack. The exception mechanism causes exceptions to propagate between these methods in the reverse order of the method invocations. The exception propagates outward from the method where the exception occurred, first to the caller of the method, then to the upper caller, and so on. Finally, it will be passed to the main method. If it is still not processed, the JVM will terminate the program and print the information of the exception trace stack.


Bonus answer:
The difference between throw and throws
:
● It can only be used in the method signature - multiple exceptions can be declared, separated by commas
● It means that the current method does not know how to handle this exception, and this exception is handled by The caller of this method handles (if the main method does not know how to handle the exception, the exception will be handed over to the JVM for processing. The way the JVM handles the exception is to print the exception trace stack information and terminate the program, which is why the program encounters The reason why the exception will end automatically)
● throws indicates a possibility of exceptions, and these exceptions may not necessarily occur
throw:
● indicates that some kind of exception object is thrown in the method, and the throw statement can be used alone.
● The throw statement throws an exception instance, not an exception class, and only one exception instance can be thrown at a time
● Executing throw must throw some kind of exception.


Questions about finally:
When a Java program executes a try block or a catch block and encounters a return or throw statement, both of these statements will cause the method to end immediately, but the system will not end the method by executing these two statements, but will look for the exception handling process Whether the finally block is included, if there is no finally block, the program will immediately execute the return or throw statement, and the method will terminate; if there is a finally block, the system will immediately start to execute the finally block. Only after the execution of the finally block is completed, the system will jump back to execute the return or throw statement in the try block and catch block again; if the return or throw statement is also used in the finally block, the finally block will terminate the method, and the system will not Jump back to execute any code in the try block, catch block. This will cause the return and throw statements in the try block and catch block to fail, so we should try to avoid using return or throw in the finally block.
Several situations where finally code blocks are not executed:
● If a thread is interrupted or killed when executing a try statement block or a catch statement block, the corresponding finally statement block may not be executed.
● If System.exit(1); is used in the try block or catch block to exit the virtual machine, the finally block will lose the chance to execute.

Guess you like

Origin blog.csdn.net/Your_Boy_Tt/article/details/130390584