/異常處理的三個方式:
1.try{}-catch{}-catch{}-....-finally{}:try中有異常時會在捕獲到的catch中進行處理
2.throws:選擇性的有異常時拋出//可拋出多個異常對象
3.method throws Exception{try-catch{throw} }//throw會直接拋出異常
public class ExceptionEx1
{
/*異常處理的三個方式:
*1.try{}-catch{}-catch{}-....-finally{}:try中有異常時會在捕獲到的catch中進行處理
*2.throws:選擇性的有異常時拋出//可拋出多個異常對象
*3.method throws Exception{try-catch{throw} }//throw會直接拋出異常
*
**/
/*
*一個try可對應多個catch
**/
public static void main(String args[])
{
Person p=null;
System.out.println ("start");
p.sage=18;
int x[]=new int[10];
try//發現一處錯誤後跳出try塊,進入catch,try中之後的語句不會再執行,也不會跳轉回來,而是執行catch之後的語句
{ //try後既沒有catch,也沒有finally,會報錯
//System.exit(0);//退出虛擬機,不會執行finally
p.sage=18;
int y=9/1;
System.out.println (x[3]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println ("數組下標越界");
int z=9/0;//try中出錯,會先執行finally再拋出錯誤
//finally中出錯會直接拋出
}catch(ArithmeticException e)
{
System.out.println ("整除0出錯");
}catch(NullPointerException e)
{
System.out.println ("空指向異常");
}catch(Exception e)//所有異常類的父類,可捕獲所有異常,如果在其他catch前會報錯
{
e.printStackTrace();//輸出異常棧信息,如果catch,這是最基本的信息,程序會繼續執行。
}finally{//沒有異常,會執行;有異常並且catch到會執行;有異常沒有catch到也會執行finally之後再拋異常
//遇到沒有catch到的異常可以有備用操作
//但try之外出錯不會執行finally
//總結:程序進入try塊後就一定會執行finally,只要不發生1,掉電或線2.程死亡以及3.System.exit(0);
System.out.println ("Finally");
}
System.out.println ("end");
}
}
class Person
{
int sage;
}
import java.net.ServerSocket;
public class ExceptionEx2
{
/*throws 和 throw 是異常的一種處理方法
**/
public static void main(String args[]) //throws Exception//功能:將錯誤上報.且此處的異常對象必須比其內部throw的對象等級高或同級
{
//ServerSocket ss=new ServerSocket(8080);//可能會出IOException,必須有備用操作(放在try中,且必須把可能出現的異常catch到)
//但如果有throws,在沒出錯時不會有編譯錯誤,出錯時直接拋出,沒有其他操作。
try
{
ServerSocket ss=new ServerSocket(8080);//可能會出IOException
ServerSocket ss2=new ServerSocket(8080);//出錯,端口被佔
}catch(RuntimeException e)
{
System.out.println (2);
//e.printStackTrace();//輸出異常棧信息
//throw new Exception();//拋出異常給上一級,如未處理,則中斷程序,也可在上一級的catch中處理。
} //如果是在try中也可由catch捕獲(由最近的catch處理)
//不同方法中出現異常的處理
/*一個方法中出現異常,本方法中catch到,正常執行;
*此方法中沒有catch到,上一級方法catch到,異常之後的語句不再執行
*若上一級方法還未catch處理,繼續上拋,直到catch到,最後沒有處理,拋出異常。
*上拋過程中異常類只能越來越高級(默認throws Exception)
**/
try
{
aa();//bb()中出現異常,但在中途未作處理,一直拋到這裏
}catch(Exception e){
e.printStackTrace();
}
try
{
diy(55);//自定義異常
}catch(RuntimeException e)
{
e.printStackTrace();
}catch(Exception e)
{
e.printStackTrace();
}
System.out.println ("main end");
}
public static void aa()
{
System.out.println ("aa start!");
bb();
System.out.println ("aa end!");
}
public static void bb()
{
System.out.println ("bb start!");
int z=9/0;
System.out.println ("aabb end!");
}
//自定義異常測驗
public static void diy(int x) throws MyException
{
if(x<0)
{
throw new MyRuntimeException("不能小於等於0:"+x);
}
if(x>12)
{
throw new MyException("不能大於12:"+x);
}
System.out.println (x);
}
}
class MyRuntimeException extends RuntimeException
{
public MyRuntimeException(String msg)
{
super(msg);
}
}
class MyException extends Exception//必須在出錯的方法內處理或者throws的異常,不能直接throw。
//屬於可能出錯的類型,必須在可能出錯處做出處理(try),或用throws,到上一級處理
{
public MyException(String msg)
{
super(msg);
}
}