try中含有throw到底是怎么回事,應(yīng)該怎么理解?
public?class?excTest?{ public?static?void?main(String[]?args)?{ try{ System.out.println("try中含有throw"); throw?new?e1(); } catch(e1?e){ System.out.println("fsasd"); System.out.println(e.getMessage()); } } }
public?class?e1?extends?Exception{ public?e1(){ super("dafsf"); } }
如上,上面這個(gè)控制臺(tái)顯示:
try中含有throw
fsasd
dafsf
這個(gè)throw應(yīng)該怎么理解?
不是把異常拋到main方法嗎?按這里的邏輯應(yīng)該是在catch塊中輸出啊!
2015-03-03
你在try中扔了一個(gè)異常,這時(shí)catch會(huì)一直等待看有沒有異常出現(xiàn)。如果捕獲到了異常就會(huì)繼續(xù)執(zhí)行并輸出異常信息
2015-01-12
try-catch是捕獲異常的語句塊,而在try中直接扔出一個(gè)異常之后,會(huì)直接進(jìn)入catch語句塊中