1. 程式人生 > >Java基礎(一)異常處理關鍵字:try catch finally throw throws

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

cut main 5.1 模型 指針 str control 情況 實例

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

都說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.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