對(duì)拋出異常這一塊比較模糊。
public?void?divide(int?one,int?two)?throws?Exception{ if(two==0) throw?new?Exception("兩數(shù)相除,除數(shù)不能為0."); else System.out.println("兩數(shù)相除結(jié)果為:"+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()); }沒(méi)有運(yùn)行,怎么回事呢?
2016-02-05
public class ExceptionTest {
public void divide(int one,int two) throws Exception{
? ?if(two==0)
? ? ? ?throw new Exception("兩數(shù)相除,除數(shù)不能為0.");
? ?else
? ? ? ?System.out.println("兩數(shù)相除結(jié)果為:"+one/two);
}
public void computer(){
? ?try{
? ? ? ?divide(5,0);
? ?}catch(Exception e){
? ? ? ?System.out.println("出現(xiàn)異常了"+e.getMessage());
? ?}finally{
? ? ? ?System.out.println("Happy New Year!");
? ?}
}
public static void main(String[] args){
? ?ExceptionTest be=new ExceptionTest();
? ?be.computer();
}
}
//運(yùn)行結(jié)果:出現(xiàn)異常了 兩數(shù)相除,除數(shù)不能為0.?
// ? ? ? Happy New Year!
這是我用你的程序試了一下,你所說(shuō)的部分運(yùn)行了 ,我覺(jué)過(guò)程應(yīng)該是divide函數(shù)拋出異常,然后computer()函數(shù)進(jìn)行捕捉 打印e.getMessage()也就是異常信息,因?yàn)槟闶侵苯虞敵鰁.getMessage() 所以和你divide函數(shù)內(nèi)寫(xiě)的異常信一樣,你誤以為是?
catch(Exception?e){
System.out.println(e.getMessage());
}沒(méi)有運(yùn)行?
2016-07-03
?System.out.println("出現(xiàn)異常了"+e.getMessage()); 這句代碼中e.getMessage()是啥意思啊 沒(méi)看懂
2016-02-05
至于意義我也不是理解的很透徹 我也是初學(xué)者 我覺(jué)得只能在以后的實(shí)際項(xiàng)目中具體理解了。