異常初探(二)

package com.liujunhua.it01;
/**
 * 對有可能出現異常方法的處理:
 * 對於有可能出現異常的方法,開發者需要捕獲該異常或者聲明該異常 聲明該異常後當其他開發者調用該方法的時候,
 * 編輯器會提醒這是一個有可能出現異常的方法,可以對其進行處理。
 */
public class Demo03 {

	public static void main(String[] args) {

		ExceptionTest exceptionTest = new ExceptionTest();

		try {
			int x = exceptionTest.div(4, 0);
			System.out.println("x = " + x);
		} catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
		}

		System.out.println("**********************");
	}

}

class ExceptionTest {

	//這裏聲明並拋出了異常
	int div(int a, int b) throws Exception {
		return a / b;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章