11.4 Exception Handling Process

Exception Handling Process

Java exception may be handled in the program are all generated abnormal operation, when the program executes a line of code to the code execution and exception occurs, the exception to this type of determination by the JVM to help the user, and automatically moving specified type the exception class object instantiation process;
if this time there are among the program does not provide support for exception handling, exception handling will be using the default JVM way, firstly print exception information, then exit the current program;
if the program at this time If there is exception handling, exception class object instance then the try statement will be generated captured;
try to capture the exception type after its abnormal matching catch the alignment performed sequentially, and if at this time the catch the same catch the exception type, you think we should use this catch exception handling. If not then continue to match subsequent catch type, without any catch the match is successful, it means that the exception can not be processed;
regardless of whether an exception handling eventually to do finally statement, but when finally completed the implementation of the program After further determine whether the current exception has been processed, if processed, and then continue to back the implementation of other code, if there is no deal then handed over to the JVM the default process.
Here Insert Picture Description
Abnormal inherit
Here Insert Picture Description
Throwable has two subclasses
Error: refers to the JVM error, this time the program is not executed, can not be processed;
Exception: refers to the abnormal program generated by the operation, the user can use exception handling formatting.

Example: simplified exception handling

package cn.mldn.demo;
public class JavaDemo {
	public static void main(String args[]) {
		System.out.println("【1】****** 程序开始执行 ******");
		try {
			int x = Integer.parseInt(args[0]);// 初始化参数转为数字
			int y = Integer.parseInt(args[1]);// 初始化参数转为数字
			System.out.println("【2】****** 数学计算:" + (x / y)) ;// 除法计算
		} catch (Exception e) {// 处理所有异常
			e.printStackTrace() ;
		} finally {// 最终出口,必然执行
			System.out.println("【F】不管是否出现异常,我都会执行。") ;
		}
		System.out.println("【3】****** 程序执行完毕 ******");
	}
}

Results of the

【1】****** 程序开始执行 ******
java.lang.ArrayIndexOutOfBoundsException: 0
【F】不管是否出现异常,我都会执行。
【3】****** 程序执行完毕 ******
	at com.lxh.elevenchapter.Java248.main(Java248.java:7)

At this time, Unified abnormal use Exception for processing, so no matter what kind of situation occurs in a program, the program can be captured and processed.

Note: When capturing multiple exceptions, catch exceptions on small exception to capture an unusually large before the exception.
When writing exception handling, Exception handling must be written after processing ArithmeticException, otherwise the syntax error.

Published 162 original articles · won praise 9 · views 3084

Guess you like

Origin blog.csdn.net/ll_j_21/article/details/104733560