Java throw和throws使用區(qū)別分析
代碼實例:
問題:為什么testRunntimeException()方法沒有出現(xiàn)編譯錯誤提示,而testCheckedException()方法卻出現(xiàn)unhandle exception?
分析:
Excepiton分兩類:checked exception、runtime exception;直接繼承自Exception就是checked exception,繼承自RuntimeException就是runtime的exception。
你可以簡單地理解checked exception就是要強制你去處理這個異常(不管你throws多少層,你終歸要在某個地方catch它);而runtime exception則沒有這個限制,你可以自由選擇是否catch。
那些強制異常處理的代碼塊,必須進行異常處理,否則編譯器會提示“Unhandled exception type Exception”錯誤警告。
這里testRunntimeException()方法是runtime exception異常,testCheckedException()方法是exception異常,屬于checked exception異常
所以testCheckedException()方法卻出現(xiàn)unhandle exception
怎么解決testCheckedException()方法卻出現(xiàn)unhandle exception?
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. python 如何在 Matplotlib 中繪制垂直線2. bootstrap select2 動態(tài)從后臺Ajax動態(tài)獲取數(shù)據(jù)的代碼3. ASP常用日期格式化函數(shù) FormatDate()4. python中@contextmanager實例用法5. html中的form不提交(排除)某些input 原創(chuàng)6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動輸入功能實現(xiàn)代碼8. 如何通過python實現(xiàn)IOU計算代碼實例9. 開發(fā)效率翻倍的Web API使用技巧10. vue使用moment如何將時間戳轉為標準日期時間格式
