Exception handling in java-try-catch-finally

When writing a program, we will encounter exception handling for the program, such as using if-else to solve the exception problem, the following problems will occur:
1. The code is bloated;
2. A lot of effort is spent on "plugging loopholes";
3. It is difficult All vulnerabilities found.
For this, Java uses the exception mechanism to provide the program with the ability to handle errors. The overall processing flow is as follows:
Insert picture description here
1. Exception definition and classification
Insert picture description here
2. Exception declaration, capture, and invocation The
exception handling in Java is implemented through 5 keywords:
try , Catch, finally, throw, throws
try: execute code that may produce exceptions
catch: catch exceptions
finally: code can always execute regardless of whether an exception occurs or not.
throw: manually throw exceptions
throws: declare various system exceptions that may be thrown in the method
(1) try-catch-finally
1. When there is no exception in the operation, the execution order: try-finally-follow-up code
Insert picture description here
2. When there is an exception and the exception in the catch can be matched , the execution order is: the code before the try exception-catch -finally-Follow-up code, when an exception occurs in the try, the code behind the try will no longer be executed, as shown in the following figure:
Insert picture description here
By the way, printStackTrace is a stack trace function, which shows the execution flow of the current class except the program.
printStackTrace(): output abnormal stack information;
getMessage(): Returns the description string of the exception information which is part of the output information of printStackTrace().
3. When there is an exception in the try but there is no match in the catch , the execution order:
code before the try exception-"catch match one by one -" program interruption
Insert picture description here
(2) When there are multiple catches, the following rules need to be paid attention to:
1) When arranging catch statements , You need to first subclass and then parent (so that the program exception thrown is clearer and clearer);
2) When an exception occurs, the program is matched in order;
3) Only the first catch that matches the exception type is executed Statement.
1. Examples
public class try_catchs { public static void main(String[] args) { int num1 = 0; int num2 = 0; double b[] ={2}; Scanner sc = new Scanner(System.in); System. out.println("Please enter two positive integers:"); try{ num1 = sc.nextInt(); num2 = sc.nextInt(); b[0] = num1/num2; System.out.println("Enter The quotient of the two numbers is "+b[0]); b[2] = num1+num2;












System.out.println("The sum of the two numbers entered is"+b[2]);
}
catch(ArithmeticException e ){ System.out.println("Please enter the second number not to be 0") ; } catch(ArrayIndexOutOfBoundsException e){ System.out.println("The length of the array you defined is not enough"); } catch(Exception e){ e.printStackTrace(); } finally { System.out.println(" abnormal external networks, have to perform I "); } System.out.println (" which is behind the try-catch-finally Code "); } } 2. 2. performed when the input 4 and 0:00, the first priority is triggered An exception: When you enter 4 and 2, the second exception is triggered: (3) The finally code block Through the examples of the above two parts, we can know that no matter whether there is an exception in the try-catch or not, the code in the finally is always Execution, but you need to pay attention to the execution of finally in the following situations: 1) The code that contains return in the try or catch, when the execution reaches the return, the finally needs to be executed first, and then return;

















Insert picture description here

Insert picture description here



2) The program execution encounters the code that exits the Java virtual machine, and finally does not execute at this time (similar to the situation where the Internet is suddenly interrupted in the process of playing an online game)
1. Code example
Insert picture description here
with return in try 2. Code example with return in catch
Insert picture description here
3 The processing program contains a statement to exit the java virtual machine
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_44801116/article/details/106040290