throws e
運用在方法標籤後面:
如下
public class Math {
public int method01(int i,int j)throws Exception{
int c =i/j;
return c;
}
}
表示將此method01方法中可能存在的Exception 拋出異常給調用此方法的方法,當前方法不做異常的處理
此時要注意,當有上級方法調用method01方法時,就一定需要對mehod01中拋出的異常進行處方法處理,否則上級類中調用method01時會報錯;
可以在上級方法中再次在方法標籤後 throws e,拋出給上上級(處理代碼一),或者在調用method01方法處進行try-catch(處理代碼二)
當方法層層調用時,需要一層層拋出,如果在main方法中拋出throws e 的話,想當於異常交由jvm處理,也相當於沒有拋出異常,因爲程序會在bug處停止,面我們利用異常機制對異常進行拋出,希望能夠在控制檯查看到錯誤信息,確不影響程序運行。
處理代碼一:
public class Math {
public int method01(int i,int j)throws Exception{
System.out.println("mehod方法開始");
int c =i/j;
System.out.println("mehod方法結束");
return c;
}
}
public class TrowsDamo2 {
public static void main(String[] args) {
Math math = new Math();
System.out.println("運算開始");
try {
int div2 = math.method01(15, 0);//此時調用Math類的method01方法
} catch (Exception e) {
System.err.println(e.getMessage());
}
System.out.println("運算結束");
}
執行結果
運算開始
mehod方法開始
/ by zero
運算結束
分析:在main方法中調用了Math類中的methdod01方法,並且method01拋出異常,在main方法中進行try-catch,對異常進行處理。(ps:如果此時main方法也對異常進行上拋 thows e 的話,程序會將異常交由jvm處理,程序會報錯)
method01方法中,“method方法結束”沒有輸出,在下層方法中,遇到異常,異常後面的代碼不再執行 代碼二可以解決此問題。
處理代碼二:
public class Math {
public int method01(int i,int j)throws Exception{//
拋出異常 System.out.println("mehod方法開始");
int c=0;
try { c =i/j; }
catch (Exception e) {
System.out.println(e.getMessage()+"---method01異常"); }
System.out.println("mehod方法結束"); return c; } }
public class TrowsDamo2 {
public static void main(String[] args) {
Math math = new Math();
System.out.println("運算開始");
try {
int div2 = math.method01(15, 0);//此時調用Math類的method01方法
} catch (Exception e) {
System.err.println(e.getMessage()); // 此處沒有捕獲到異常
}
System.out.println("運算結束");
}
}
輸出結果
運算開始
mehod方法開始
/ by zero---method01異常
mehod方法結束
運算結束
分析
method01方法對外拋出異常,但方法內部使用try-catch進行捕捉異常,異常在catch塊已處理,不會再對外拋出異常,所以main方法中的try-catch中未捕捉到異常未打印出異常信息
雖然代碼二中,實現了在異常後代碼的執行,卻沒有辦法實現異常的上拋。
throw e
一般配合try-catch 使用
格式如下
public class ThisDemo06{
public static void main(String args[]){
try{
throw new Exception("自己拋着玩的。") ;// 拋出異常的實例化對象
}catch(Exception e){
System.out.println(e) ;
}
}
throw e 實現了在try-catch塊中對異常的上拋,catch塊中不再對異常進行處理 在catch塊中,throw e後不能再跟代碼 ,且cathc塊外,後面的代碼不被執行
以下錯誤例子
catch (Exception e) {
throw e; //下面這句打印會編譯報錯,throw後不能加指令
System.out.println(e.getMessage());
}finally {
System.out.println("finally塊中必然執行");
//finally塊會執行
}
System.out.println("此命令不會執行");
//throw後,命令不執行
特別說明:
當層層調用時,如果是都是使用在方法簽名後使用throws拋出異常,下層代碼中,任何一層出現bug,會在bug處拋出異常,bug代碼後的指令將不會執行。
使用try-catch塊中,throw拋出異常,throw後的代碼也將不會再執行
以下爲例子
public class Math {
public int method01(int i,int j){//拋出異常
System.out.println("mehod方法開始");
int c=0;
try {
c =i/j;
} catch (Exception e) {
System.out.println(e.getMessage());
throw e; //下面這句打印會編譯報錯,throw後不能加指令
}finally {
System.out.println("finally塊中必然執行");
//finally塊會執行
}
System.out.println("此命令不會執行");
//throw後,命令不執行
return c;
}
}
public class TrowsDamo5 {
public static void main(String[] args) {
try {
new TrowsDamo5().grandSonMethod();
} catch (Exception e) {
System.err.println("rer");
}
System.err.println("stop");
}
public void grandSonMethod() throws Exception{
Math math = new Math();
System.out.println("運算開始");
try {
int div2 = math.method01(15, 0);//此時調用Math類的method01方法
} catch (Exception e) {
System.err.println("imfool");
throw e;
}
System.out.println("運算結束");
}
}
運算開始
mehod方法開始
/ by zero
finally塊中必然執行
imfool
stop
這個代碼稍微複雜點,我使用流程圖來描述
ps:Exception與runtimeException區別
觀察以下代碼:
package methoud;
public class ThisDemo06{
public static void main(String args[]){
String str = "123" ;// 定義字符串,全部由數字組成
int temp = Integer.parseInt(str) ; // 將字符串變爲int類型
System.out.println(temp * temp) ;// 計算乘方
}
};
public static int parseInt(String s) throws NumberFormatException
此方法明明使用了throws關鍵字拋出異常,爲什麼不用處理,也可以編譯通過?
在JAVA異常處理機制中,
1)如果拋出的是EXception的類型,則必須進行try ..catch進行處理。
2)如果拋出的是RuntimeException的類型,則可以不使用try。。catch處理,一旦發生異常之後,將由JVM處理。
爲了保證程序的健康性,在有可能出現異常的時候還是老實使用try ..catch處理。
————————————————
版權聲明:本文爲CSDN博主「_大帥_」的原創文章,遵循CC 4.0 by-sa版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/ted_cs/article/details/82355549