11.3 处理多个异常

发布时间 2023-07-01 23:46:35作者: 盘思动

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 (ArithmeticException e) {							// 数学异常 10 0
			e.printStackTrace() ;
		} catch (NumberFormatException e) {						// 数字格式化异常 a 0
			e.printStackTrace() ;
		} catch (ArrayIndexOutOfBoundsException e) {				// 数组越界异常 不输入任何值;
			e.printStackTrace() ;
		} finally {												// 最终出口,必然执行
			System.out.println("【F】不管是否出现异常,我都会执行。") ;
		}
		System.out.println("【3】****** 程序执行完毕 ******");//---这里不一定执行,如果有异常没有处理就不执行;
	}
}