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就是要強(qiáng)制你去處理這個異常(不管你throws多少層,你終歸要在某個地方catch它);而runtime exception則沒有這個限制,你可以自由選擇是否catch。
那些強(qiáng)制異常處理的代碼塊,必須進(jìn)行異常處理,否則編譯器會提示“Unhandled exception type Exception”錯誤警告。
這里testRunntimeException()方法是runtime exception異常,testCheckedException()方法是exception異常,屬于checked exception異常
所以testCheckedException()方法卻出現(xiàn)unhandle exception
怎么解決testCheckedException()方法卻出現(xiàn)unhandle exception?
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
