對拋出異常這一塊比較模糊。
public?void?divide(int?one,int?two)?throws?Exception{ if(two==0) throw?new?Exception("兩數相除,除數不能為0."); else System.out.println("兩數相除結果為:"+one/two); } public?void?computer(){ try{ divide(5,0); }catch(Exception?e){ System.out.println(e.getMessage()); }finally{ System.out.println("Happy?New?Year!"); } } public?static?void?main(String[]?args){ BlueException?be=new?BlueException(); be.computer(); } 這一段catch(Exception?e){ System.out.println(e.getMessage()); }沒有運行,怎么回事呢?
2016-02-05
public class ExceptionTest {
public void divide(int one,int two) throws Exception{
? ?if(two==0)
? ? ? ?throw new Exception("兩數相除,除數不能為0.");
? ?else
? ? ? ?System.out.println("兩數相除結果為:"+one/two);
}
public void computer(){
? ?try{
? ? ? ?divide(5,0);
? ?}catch(Exception e){
? ? ? ?System.out.println("出現異常了"+e.getMessage());
? ?}finally{
? ? ? ?System.out.println("Happy New Year!");
? ?}
}
public static void main(String[] args){
? ?ExceptionTest be=new ExceptionTest();
? ?be.computer();
}
}
//運行結果:出現異常了 兩數相除,除數不能為0.?
// ? ? ? Happy New Year!
這是我用你的程序試了一下,你所說的部分運行了 ,我覺過程應該是divide函數拋出異常,然后computer()函數進行捕捉 打印e.getMessage()也就是異常信息,因為你是直接輸出e.getMessage() 所以和你divide函數內寫的異常信一樣,你誤以為是?
catch(Exception?e){
System.out.println(e.getMessage());
}沒有運行?
2016-07-03
?System.out.println("出現異常了"+e.getMessage()); 這句代碼中e.getMessage()是啥意思啊 沒看懂
2016-02-05
至于意義我也不是理解的很透徹 我也是初學者 我覺得只能在以后的實際項目中具體理解了。