java異常——五個關鍵字(try、catch、finally、throw、throws)
阿新 • • 發佈:2018-08-18
五個 code print 聲明 col span pan all 組合
一、try、catch、finally常用組合
-
try{ xxx }catch(xxxException e){ e.printStackTrace(); }
-
try{ xxx }catch(xxxException e){ e.printStackTrace(); }finally{ System.out.println("無論會不會拋異常,我就是會輸出Σ(っ°Д°;)っ"); }
-
try{ xxx }catch(AException e){ e.printStackTrace(); }catch(BException e){ e.printStackTrace(); } ...
-
try{ xxx }finally{ System.out.println("無論會不會拋異常,我就是會輸出Σ(っ°Д°;)っ"); }
二、throw與throws的區別
- throw:在方法內使用,手動拋出異常
- throws:在方法聲明時使用,聲明可能會拋出的異常。
java異常——五個關鍵字(try、catch、finally、throw、throws)