首頁  >  文章  >  Java  >  Java基礎值:異常處理關鍵字...

Java基礎值:異常處理關鍵字...

怪我咯
怪我咯原創
2017-06-26 11:46:531359瀏覽

都說java 語言是非常健壯性 如:垃圾回收機制、記憶體模型、異常處理,強型別轉換、跨平台,等等,使得Java語言的受到青睞。今天我們先來聊聊java的異常處理機制try catch finally throw throws,平常我們貌似小瞧了這五個關鍵字。開發應用系統,良好異常的處理對系統後期的開發、維護、升級、使用者的體驗尤其重要。

異常有個非常重要的特徵,從出現異常的位置到最頂端的main方法如果你一直沒catch到它,最終jvm會幫你拋出異常訊息,糟糕的是該線程斷掉,後續程式碼也不再執行,從此無聲無息的消失在jvm這片汪洋大海。前面我公司的一個專案前端ajax請求control做支付,由於control的catch裡面拋出了一個空指針,導致前端頁面卡住不動了,解決方案:由於control層一般是頂層最好catch到任何有可能出現異常的地方,其他非頂層還可以繼續throw 或throws往上拋。還有就是最好為每個ajax設定超時時間。

 

先簡單介紹下throw 跟throws:

throw :在方法體內拋出一個異常,實際存在的異常對象,一般用是自己擴展的異常實例,throws是放在方法名稱後面,表示該方法如果出現異常, 我不想處理或處理不了,交給呼叫者處理,可以thow拋出一個運行時異常(unchecked)如ClassNotFoundException,NumberFromartException,   也可以throws或throw +try或throw+throws 處理一個checked異常如:IOExcepion,SocketException、繼承exception類別之類的異常, 。差別在於checked異常必須處理(要么try,要么throws繼續往上拋,否則編譯是通不過的),而運行時異常可以不處理,一直不處理的後果是出現異常後jvm報出異常信息然後線程斷掉。 throw 跟throws關鍵字一般情況不建議在程式碼中使用,推薦所有異常就地解決。知道用就行了,不做過多的講解。

try的組合規則:1, try{}catch(){}  2,try{}catch(){}finally{}  3,try{}finally{}    ,1跟2的方式catch可以有多個

 

朋友,吃幾顆栗子:

#1,無try組合 

public class CatchExecuteJustOne {
  public void methodOne( ){
    System.out.println("into methodOne method");
    int one=1/0;
    System. try組合,報錯後執行緒已經斷掉
}
  public static void main(String[] args) {
    CatchExecuteJustOneS cejo = new CatchExecuteJustOneS();#dOneS( );
    System.out.println("end  main method"); //不會輸出 沒有try組合 報錯線程已經斷掉

  }

#}
##輸出:

Into methodOne method

Exception in thread "main" java.lang.ArithmeticException: / by zero

at priv.lilei.exception.example_1.CatchExecuteJustOneS.methodOne(CatchExecuteJustOneS.16wpriv) lilei.exception.example_1.CatchExecuteJustOne.main(CatchExecuteJustOne.java:19)


 

2.1,有try組合案例1

public class CatchExecuteJustOne {#1] 方法。 out.println("methodOne try到");

  }

  System.out.println("end  methodOne method");

}


  public static void main(String[] }

  public static voidoid main(String[]   ) {
    CatchExecuteJustOne cejo = new CatchExecuteJustOne();
    cejo.methodOne();
 總     
# 輸出:

into methodOne method
methodOne try到
end  methodOne method
end  main method

2.2,有try組合案例2

# public class CatchExecuteJustOne {

public void methodOne(){

  System.out.println("into methodOne method");

  int one=1/0;

  System. methodOne method"); //不會執行線程上面報錯斷掉直接拋出了
}

public static void main(String[] args) {

  try{

    CatchExecuteJustOne ExecuteJustOne ExecuteJustOne ExecuteJustOne = new CatchExecuteJustOne();

    cejo.methodOne();

  }catch(Exception exception){
    System.out.println("in##     System.out.println("in##     System。報的異常
  }
    System.out.println("end  main method"); //會執行 try到上面方法報的異常
#  }
#}
## 輸出:

into methodOne method
into main method catch
end  main method

 

2.3,有try案例組合3    異常只會被最近捕捉到它的catch 一次。像switch case 跟if() if else(){} if()else if{} 語法一樣

 

public class CatchExecuteJustOne {
public void methodOne(){
# System.out.println("into methodOne method");
try{
int one=1/0;
}catch(ArithmeticException e){
System.out.println("catch 1 ");
}catch (Exception e) {
System.out.println("catch 2");//不會執行已經執行了上一個catch 1
}
}

public static void main(String[] args) {
CatchExecuteJustOne cejo = new CatchExecuteJustOne();

try {
cejo.methodOne();
} catch (Exception e) {
System.out.println("man catch");//不會執行已經執行了catch 1
}

System.out.println("end main method") ;
}
}

輸出:

into methodOne method
catch 1
end main method

2.4 有try組合案例4,  try{}finally{}組合,finally沒中有回傳值得時候執行緒會斷掉,但在finally中有回傳值時候執行緒不會斷掉只是後續程式碼不會執行, 這種組合建議少用。

 //沒回傳值

public class CatchExecuteJustOne {
  public void methodOne(){  //沒回傳值
      。 }
    System.out.println("end  methodOne method"); //不會執行線程上面報錯斷掉直接拋出了
}

public static void main(String[] args ) {
  CatchExecuteJustOne cejo = new CatchExecuteJustOne();
  cejo.methodOne();
  System.out.println("end  main method");// System.out.println("end  main method");/出了
  }
}

沒回傳值的輸出:

into methodOne method
Exception in thread "main" into methodOne finally

java.lang. ArithmeticException: / by zero

at priv.lilei.exception.example_1.CatchExecuteJustOne.methodOne(CatchExecuteJustOne.java:14)

at priv.lilei.exception.example_1.CatchOne2Just.


##有回傳值:


public class CatchExecuteJustOne {

public String methodOne(){

System.out.println("into methodOne method"); try{ System.out.println("1");

int one=1/0;

System.out.println("2");//不會執行執行緒上面報錯斷掉直接拋出了
}finally{
System.out.println("into methodOne finally");//會輸出
return "1";
}
}

public static void main(String[] args) {
CatchExecuteJustOne cejo = new CatchExecuteJustOne();
cejo.methodOne();
System.out.println("end  main method" );//會執行因為上面有try到且方法有回傳值
}
}

有傳回值的輸出:

into methodOne method
1
into methodOne finally

end  main method

 


2.5,帶finally的組合 finally永遠被執行,有返回值得情況在返回之前執行,  除非出現特別暴力的行為如system.exit(0); 或者斷掉了,或者內存溢出了等Error錯誤。

return 組合

2.5.1 以下兩個案例 在沒有異常跟有異常的情況 ,在catch跟finally 中給予變數再次賦值  有差異。沒有異常再次賦值失敗,而有異常再次賦值成功。

 

1 沒有異常的情況 

public class CatchExecuteJustOne {

public String methodOne(){

String a =""; System.out.println("into methodOne method"); try{

a="a";

return a;
}catch(ArithmeticException e){
System.out.println("catch 1");
}finally {
System.out.println(1);
a="a2";        //不錯誤的情況  不會賦值給a;
System.out.println(2);
}
System.out.println(3); //不會執行上面return a方法已經回傳了
return a;
}

public static void main(String[] args) {
CatchExecuteJustOne cejo = new CatchExecuteJustOne();
System.out.println(cejo.methodOne());
}
}

try中return 沒有異常的情況的輸出:

into methodOne method
1
2

a

#2 有異常的狀況

public class CatchExecuteJustOne {
public String methodOne(){
String a="";
System.out.println("into methodOne method");
try{
a ="a";
int i=1/0;
return a;
}catch(ArithmeticException e){
System.out.println("catch 1");
} finally {
System.out.println(1);
a="a2";   //有例外會重新賦值給a 變數
System.out.println(2);
}
System.out.println(3); //會輸出捕捉到了異常沒有從上面第一個return a返回而是從下面這個return返回
return a;
}

public static void main(String[] args) {
CatchExecuteJustOne cejo = new CatchExecuteJustOne();
System.out.println(cejo.methodOne());
}
}

}}

#try中return 有異常的狀況輸出:

into methodOne method
catch 1
1
2

3

a2

#### ###

以上是Java基礎值:異常處理關鍵字...的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn