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");
}
}