runxinzhi.com
首页
百度搜索
检查异常和未检查异常不同之处
public
class
ExceptionTypeTest {
public
void
doSomething()
throws
ArithmeticException{
System.out.println();
}
public
static
void
main(){
ExceptionTypeTest ett =
new
ExceptionTypeTest();
ett.doSomething();
}
}
问题1:上面的程序能否编译通过?并说明理由。
解答:能编译通过。分析:按照一般常理,定义doSomething方法是定义了ArithmeticException异常,在main方法里 里面调用了该方法。那么应当继续抛出或者捕获一下。但是ArithmeticException异常是继承RuntimeException运行时异常。 java里面异常分为两大类:checkedexception(检查异常)和unchecked exception(未检
查异常),对于未检查异常也叫RuntimeException(运行时异常),对于运行时异常,java编译器不要求你一定要把它捕获或者一定要继续抛出,但是对checkedexception(检查异常)要求你必须要在方法里面或者捕获或者继续抛出.
问题2:上面的程序将ArithmeticException改为IOException能否编译通过?并说明理由。
解答:不能编译通过。分析:IOException extends Exception 是属于checked exception,必须进行处理,或者必须捕获或者必须抛出
总结:java中异常分为两类:checked exception(检查异常)和unchecked exception(未检查异常),对于未检查异常也叫RuntimeException(运行时异常).
对未检查的异常(unchecked exception )的几种处理方式:
1、捕获
2、继续抛出
3、不处理
对检查的异常(checked exception,除了RuntimeException,其他的异常都是checked exception )的几种处理方式:
1、继续抛出,消极的方法,一直可以抛到java虚拟机来处理
2、用try...catch捕获
注意,对于检查的异常必须处理,或者必须捕获或者必须抛出
相关阅读:
SpringMVC 注解大全
Maven实战--- dependencies与dependencyManagement的区别
SpringMVC5.2.0 使用到 WebDataBinderFactory.createDataBinder 方法的类
Spring DataBinder
mysql 查询主键外键
objectMapper.canSerialize SpringMVC实体转JSON字符串
整合SSM时启动Tomcat报nested exception is java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException
synchronizeOnSession
init-method afterPropertiesSet BeanPostProcessor 执行顺序
SpringMVC中重定向参数的使用以及原理解析 RedirectAttributes
原文地址:https://www.cnblogs.com/duyinqiang/p/5183935.html
最新文章
深度学习论文翻译解析(七):Support Vector Method for Novelty Detection
深度学习论文翻译解析(六):MobileNets:Efficient Convolutional Neural Networks for Mobile Vision Appliications
tensorflow学习笔记——ResNet
tensorflow学习笔记——GoogLeNet
tensorflow学习笔记——VGGNet
tensorflow学习笔记——AlexNet
@RequestPart-同时上传文件和json的解决方案
Cordova自定义插件开发
Mybatis涉及的设计模式
vue-router 基本使用
热门文章
java.awt.headless 模式
SpringBoot 启动挂载点说明 对应Spring应用事件监听
SpringMVC 配置视图解析器
SpringMVC 静态资源配置 css js img等
SpringMVC favicon.ico 配置
基于消息队列的分布式事务解决方案
AbstractRoutingDataSource -- Spring提供的轻量级数据源切换方式
HTTP协议中的Accept与Content-Type的区别
Spring-web源码解析之ContentNegotiationStrategy
Spring5--@Indexed注解 Spring使用@Indexed加快启动速度
Copyright © 2020-2023
润新知