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?

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
SpringBoot整合Swagger和Actuator的使用教程詳解
Swagger 是一套基于 OpenAPI 規(guī)范構建的開源工具,可以幫助我們設計、構建、記錄以及使用 Rest API。本篇文章主要介紹的是SpringBoot整合Swagger(API文檔生成框架)和SpringBoot整合Actuator(項目監(jiān)控)使用教程。感興趣的朋友一起看看吧2019-06-06
Java中的HttpServletRequest接口詳細解讀
這篇文章主要介紹了Java中的HttpServletRequest接口詳細解讀,是一個接口,全限定名稱為Jakarta.Serclet.http.HttpServletRequest2023-11-11
HttpServletRequest接口是Servlet規(guī)范的一員,需要的朋友可以參考下

