checked exception和unchecked exception区别
来源:互联网 发布:阿里小号怎么登录淘宝 编辑:程序博客网 时间:2024/06/01 09:52
http://blog.csdn.net/yuefengyuan/article/details/6204317
一、
Java 中定义了两类异常:
1) Checked exception: 这类异常都是Exception的子类 。异常的向上抛出机制进行处理,如果子类可能产生A异常,那么在父类中也必须throws A异常。可能导致的问题:代码效率低,耦合度过高。C#中就没有使用这种异常机制。
2) Unchecked exception: 这类异常都是RuntimeException的子类,虽然RuntimeException同样也是Exception的子类,但是它们是特殊的,它们不能通过client code来试图解决,所以称为Unchecked exception 。
另外:
error 表示恢复不是不可能但很困难的情况下的一种严重问题。比如说内存溢出。不可能指望程序能处理这样的情况。
exception 表示一种设计或实现问题。也就是说,它表示如果程序运行正常,从不会发生的情况
二、
checked exception是需要强制catch的异常,你在调用这个方法的时候,你如果不catch这个异常,那么编译器就会报错,比如说我们读写文件的时候会catch IOException,执行数据库操作会有SQLException等
UnChecked Exception是RuntimeException,也就是说运行时的异常,这种异常不是必须需要catch的,你是无法预料的,比如说你在调用一个list.szie()的时候,如果这个list为null,那么就会报NUllPointerException,而这个异常就是RuntimeException,也就是UnChecked Exception
三、
Error和RuntimeException及其子类是unchecked exception.其他exception是checked exception.
checked exception可以出现在throws子句中,unchecked exception不可以。
Error是java自己的错误或者诸如内存耗尽等严重错误,是不可抗拒的,显然没有捕捉的必要,而且也没有办法捕捉。
RuntimeException是你的程序有逻辑错误,是程序员应该积极避免其出现的异常。比如NullPointerException等,完全是程序员马虎出的错。当遇到这种错误时,java将这个错误自动捕捉到,比如显示到concole里,然后继续运行。而checked exception如果不捕捉则会导致程序终止。
四、error和excption的区别
Error的继承关系:
java.lang.Object
--java.lang.Throwable
--java.lang.Error
Exception的继承关系:
java.lang.Object
--java.lang.Throwable
--java.lang.Exception
二者的不同之处:
Exception:
1.可以是可被控制(checked) 或不可控制的(unchecked)
2.表示一个由程序员导致的错误
3.应该在应用程序级被处理
Error:
1.总是不可控制的(unchecked)
2.经常用来用于表示系统错误或低层资源的错误
3.如何可能的话,应该在系统级被捕捉
- checked exception和unchecked exception区别
- checked exception和unchecked exception区别
- checked exception 和 unchecked exception区别
- Unchecked Exception和Checked Exception的区别
- checked Exception和unchecked Exception的区别
- checked exception和unchecked exception的区别
- checked exception和unchecked exception
- Unchecked Exception 和 Checked Exception
- checked 和 unchecked exception
- Java Checked 和 UnChecked Exception 的区别
- java中的Checked Exception和Unchecked Exception的区别
- unchecked Exception & checked Exception
- unchecked Exception & checked Exception
- Unchecked Exception 和 Checked Exception 比较
- Unchecked Exception 和 Checked Exception 比较
- Unchecked Exception 和 Checked Exception 比较
- Unchecked Exception 和 Checked Exception 比较
- Java中的checked exception和unchecked exception
- android自定义日期选择
- python笔记
- nosql数据库与内存数据库
- PAT(甲级)1081
- [iOS开发项目-5] 图片浏览器
- checked exception和unchecked exception区别
- Java注解的使用@interface:
- Python解释器的探讨:第二部分代码对象
- python re.search 和 re.match 正则表达式
- 使用SQL大全(技巧讲解)
- 使用Soap头自定义身份验证
- Android学习之HelloWorld
- i2C和spi比较
- [C#]readonly字段