Java throw和throws使用区别分析
代码实例:

问题:为什么testRunntimeException()方法没有出现编译错误提示,而testCheckedException()方法却出现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()方法却出现unhandle exception
怎么解决testCheckedException()方法却出现unhandle exception?

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
SpringBoot整合Swagger和Actuator的使用教程详解
Swagger 是一套基于 OpenAPI 规范构建的开源工具,可以帮助我们设计、构建、记录以及使用 Rest API。本篇文章主要介绍的是SpringBoot整合Swagger(API文档生成框架)和SpringBoot整合Actuator(项目监控)使用教程。感兴趣的朋友一起看看吧2019-06-06
Java中的HttpServletRequest接口详细解读
这篇文章主要介绍了Java中的HttpServletRequest接口详细解读,是一个接口,全限定名称为Jakarta.Serclet.http.HttpServletRequest2023-11-11
HttpServletRequest接口是Servlet规范的一员,需要的朋友可以参考下


最新评论