Java基礎(一)異常處理關鍵字:try catch finally throw throws

來源:互聯網
上載者:User

標籤:error   dex   自己   推薦   erro   span   null 指標   語言   先來   

  嗨咯,大家晚上好,我的部落格首篇開始了 ,我們一起加油吧!

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

異常有個非常重要的特徵,從出現異常的位置 到 最頂端的main方法 如果你一直沒catch到它,最終jvm會幫你拋出異常資訊,糟糕的是該線程斷掉,後續代碼也不再執行,從此無聲無息的消失在jvm這片汪洋大海。前面我公司的一個項目前端ajax請求control做支付,由於control的catch裡面拋出了一個null 指標,導致前端頁面卡住不動了,解決方案:由於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.out.println("end methodOne method"); //不會輸出  沒有try組合,報錯後線程已經斷掉
}
  public static void main(String[] args) {
    CatchExecuteJustOneS cejo = new CatchExecuteJustOneS();
    cejo.methodOne();

    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.java:6)
at priv.lilei.exception.example_1.CatchExecuteJustOne.main(CatchExecuteJustOne.java:19)

 

2.1,有try組合案例1

public class CatchExecuteJustOne {
  public void methodOne(){
    System.out.println("into methodOne method");
  try{
    int one=1/0;
  }catch(Exception e){
    System.out.println("methodOne try到");
  }
  System.out.println("end methodOne method");
}

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

 輸出:

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.out.println("end methodOne method"); //不會執行線程上面報錯斷掉直接拋出了
}

public static void main(String[] args) {
  try{
    CatchExecuteJustOne cejo = new CatchExecuteJustOne();
    cejo.methodOne();
  }catch(Exception exception){
    System.out.println("into main method catch"); //會執行 try到上面方法報的異常
  }
    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("into methodOne method");
  try{
    int one=1/0;
  }finally{
    System.out.println("into methodOne finally");
  }
    System.out.println("end methodOne method"); //不會執行線程上面報錯斷掉直接拋出了
}

public static void main(String[] args) {
  CatchExecuteJustOne cejo = new CatchExecuteJustOne();
  cejo.methodOne();
  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.CatchExecuteJustOne.main(CatchExecuteJustOne.java:23)

有傳回值:

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基礎(一)異常處理關鍵字:try catch finally throw throws

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.