天天看點

[J2SE]s02e03.兩個錯誤處理的例子(課後練習)

<span style="font-size:18px;">package javastudy;

public class test {

	public static void main(String[] args) {
//		String s="hello";
//		try {
//			int i=Integer.parseInt(s);//try塊中是可能引起錯誤的代碼
//									  //catch塊中是出錯後執行的代碼	
//		} catch (Exception e) {
//			// TODO: handle exception
//			//System.out.println("出錯了!");
//			//e.getStackTrace();
//			System.out.println(e.getMessage());
//			
//		}
		try {
			Class.forName("javastudy.test");//動态加載
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
</span>
           

圖1.字元串轉換為整形,運作

[J2SE]s02e03.兩個錯誤處理的例子(課後練習)

圖2.變量改為“hello”,運作

[J2SE]s02e03.兩個錯誤處理的例子(課後練習)

圖3.加入錯誤處理 try catch

[J2SE]s02e03.兩個錯誤處理的例子(課後練習)

圖4.上面的錯誤可處理可不處理(字元串轉換整數),而下面是錯誤必須處理的情況(動态加載)

[J2SE]s02e03.兩個錯誤處理的例子(課後練習)
[J2SE]s02e03.兩個錯誤處理的例子(課後練習)
[J2SE]s02e03.兩個錯誤處理的例子(課後練習)

繼續閱讀