異常類中throw單獨存在的特點

public class Demo {
//在這裏將throw拋出的異常進行了封裝
	public static void show_exception() throws Exception 
	{
		throw new Exception();
	}
	public static void main(String[] args)
	{
		try
		{
			show_exception();  //說明異常可能出生可能不發生
			System.out.println("aa");
		}
		catch(Exception ex)
		{
			System.out.println("bb");
		}
		finally
		{
			System.out.println("cc");
		}
		System.out.println("dd");
	}
}

//注意這兩個類中throw拋出異常的不同之處
public class Demo2 {

	public static void func()
	{
		try
		{
			throw new Exception();
			//當throw單獨存在時,下面不能定義 語句,否則編譯失敗
			//System.out.println("ff");    
		}
		catch(Exception ex)
		{
			System.out.println("aa");
		}
	}
	public static void main(String[] args)
	{
		try
		{
			func();
		}
		catch(Exception ex)
		{
			System.out.println("bb");
		}
		System.out.println("cc");
	}
}

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