天天看點

Java 異常的捕獲及處理(異常處理标準格式)

範例:

class MyMath {
	public static int div(int x, int y) throws Exception {
		int result = 0;
		System.out.println("1.除法計算開始....");
		try {
			result = x / y;
		} catch (Exception e) {
			throw e;// 繼續抛出異常
		} finally {
			System.out.println("2.除法計算結束....");
		}
		return result;
	}
}

public class Test {
	public static void main(String[] args) {
		try {
			System.out.println("計算結果:" + MyMath.div(10, 0));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
           
Java 異常的捕獲及處理(異常處理标準格式)