異常習題

package com.test.code;

public class Test3{
	public static void foo(int i){
		try{
			if(i == 1){
				System.out.print("0");
				throw new Exception(); //執行拋出異常時catch會捕獲此異常
			//	System.out.print("00");	//不可達代碼
			}
			System.out.print("1");	//if中拋出異常時,不會執行該語句
		}catch(Exception e){
			System.out.print("2");
		}finally{
			System.out.print("3");
		}
		System.out.print("4");
	}
		
	public static void main(String args[]){
		System.out.print("參數爲1時輸出結果爲:");
		foo(1); //輸出0234
		System.out.print("\n參數不爲1時輸出結果爲:");
		foo(2);//輸出134
	}
}

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