11.2 Exception Handling

Three core keyword handling exceptions: try, catch, finally.
Exception Handling Format:

try {
	// 有可能出现异常的语句
} [ catch (异常类型 对象) {
	// 异常处理 ;
} catch (异常类型 对象) {
	// 异常处理 ;
} catch (异常类型 对象) {
	// 异常处理 ;
} .... ] [finally {
	; 不管是否出现异常,都执行的统一代码
}]

Here Insert Picture Description
Example: Exception Handling

public class Java244 {
       public static void main(String[] args) {
		   System.out.println("-----程序执行开始-----");
		   try{
			   System.out.println(10/0);         //执行除法计算
		   }catch(ArithmeticException e){      //捕捉算法异常
			   System.out.println(e);          //处理异常
		   }
		   System.out.println("-----程序执行结束-----");
	}
}

Results of the

-----程序执行开始-----
java.lang.ArithmeticException: / by zero (catch执行处理语句)
-----程序执行结束-----

When the math program in computational exception, the exception will be try statements captured, processed and handed over to catch, this time the program will end normally, and interruption execution does not occur.
However, this method is not described explicitly described type of the abnormality, and abnormal purpose is to solve the abnormality, it is possible to perform exception handling, exception classes may be used provided ** printStackTrace () ** method exception information the complete output.
Example: For a complete exception message

e.printStackTrace ()

public class Java244 {
       public static void main(String[] args) {
		   System.out.println("【1】-----程序执行开始-----");
		   try{
			   System.out.println("【2】"+10/0);         //执行除法计算
		   }catch(ArithmeticException e){      //捕捉算法异常
			   e.printStackTrace();         //处理异常
		   }
		   System.out.println("【3】-----程序执行结束-----");
	}
}


Results of the

【1】-----程序执行开始-----
java.lang.ArithmeticException: / by zero
【3】-----程序执行结束-----
	at com.lxh.elevenchapter.Java244.main(Java244.java:7)

Using this method the output exception information, we will clearly tell the user which line of code is abnormal, with user-friendly debugging and exclude abnormal operation code.

Using a finally block entry program execution, regardless of whether there is an abnormal code This code will be executed.
Example: Using finally statement

public class Java244 {
       public static void main(String[] args) {
		   System.out.println("【1】-----程序执行开始-----");
		   try{
			   System.out.println("【2】"+10/0);         //执行除法计算
		   }catch(ArithmeticException e){      //捕捉算法异常
			   e.printStackTrace();         //处理异常
		   }finally {
			   System.out.println("【F】不管是不是出现异常,我都会执行");//最终出口,必然执行
		   }
		   System.out.println("【3】-----程序执行结束-----");
	}
}

The results `

【1】-----程序执行开始-----
【F】不管是不是出现异常,我都会执行
【3】-----程序执行结束-----
java.lang.ArithmeticException: / by zero
	at com.lxh.elevenchapter.Java244.main(Java244.java:7)

Exception handling process, regardless of whether an exception code that will execute the finally block appears.
Question: The role finally statement is not small?
Answer: two different enforcement mechanisms, the role finally statement release operation is often carried out some resources in development.

Published 162 original articles · won praise 9 · views 3091

Guess you like

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