11.3 handle multiple exceptions

Capturing anomalies during the processing, each of the try statement may be provided a plurality of catch statement for a variety of different types of exception traps
Example: capturing a plurality of exception

public class Java246 {
	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 (ArithmeticException e) {	// 数学异常
			e.printStackTrace() ;
		} catch (NumberFormatException e) {// 数字格式化异常
			e.printStackTrace() ;
		} catch (ArrayIndexOutOfBoundsException 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.Java246.main(Java246.java:7)
Published 162 original articles · won praise 9 · views 3087

Guess you like

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