11.7 异常处理模型

发布时间 2023-07-02 11:37:30作者: 盘思动

demo1 这种模型,开发中经常用



class MyMath {
	public static int div(int x, int y) throws Exception {		// 异常抛出
		int temp = 0;
		System.out.println("*** 【START】除法计算开始 ***");		// 开始提示信息
		try {
			temp = x / y;									
		} catch (Exception e) {//---这里的catch可以省略,结果相同!!!
			throw e; 										// 手动抛出---抛出捕获到的异常对象
		} finally {
			System.out.println("*** 【END】除法计算结束 ***");	// 结束提示信息
		}
		return temp;											
	}
}

public class JavaDemo {
	public static void main(String args[]) {
		try {
			System.out.println(MyMath.div(10, 0));				// 调用计算方法
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}