Java - Checked vs Unchecked Exception

转载 2015年07月09日 16:41:45

http://javarevisited.blogspot.de/2011/12/checked-vs-unchecked-exception-in-java.html

Sometimes I can't open this page without proper proxy configuration, so I have to copy the content here.


What is Checked Exception in Java?

Checked Exception in Java is all those Exception which requires being catches and handled during compile time. If Compiler doesn’t see try or catch block handling a Checked Exception, it throws Compilation error.

All the Exception which are direct sub Class of Exception but not inherit RuntimeException are Checked Exception.
Example of checked Exception in Java API

IOException
SQLException
DataAccessException
ClassNotFoundException
InvocationTargetException

What is Unchecked Exception in Java?

Unchecked Exception in Java is those Exceptions whose handling is not verified during Compile time. Unchecked Exceptions mostly arise due to programming errors like accessing method of a null object, accessing element outside an array bonding or invoking method with illegal arguments. In Java, Unchecked Exception is direct sub Class of RuntimeException.

What is major benefit of Unchecked Exception is that it doesn't reduce code readability and keeps the client code clean.

Example of unchecked Exception in Java API

NullPointerException
ArrayIndexOutOfBound
IllegalArgumentException
IllegalStateException

Difference between Checked and Unchecked Exception in Java

1) Checked Exception is required to be handled by compile time while Unchecked Exception doesn't.

2) Checked Exception is direct sub-Class of Exception while Unchecked Exception are of RuntimeException.

3) CheckedException represent scenario with higher failure rate while UnCheckedException are mostly programming mistakes.

相同点:

Both Checked and Unchecked Exception are handled using keyword try, catch and finally.

Improved exception handling using multi-catch block (JDK 1. 7 feature)

With JDK7 multi catch block we can catch multiple exception in one catch block separated by pipe (|) and reduce the code duplication.


异常管理 - Java异常处理分析 (Unchecked Exception VS Checked Exception)

Java异常指定是程序在运行过程中出现错误或异常, 超出程序处理范围, 一旦出现异常, 如果不处理, 就没有必要继续运行下去, JAVA有两种类型的异常 检查异常 (Checked Excepti...
  • piaohai
  • piaohai
  • 2015年12月23日 15:55
  • 271

Unchecked Exception 和 Checked Exception 比较

Throwable类是所有异常的始祖,它有两个直接子类Error / Exception:    Error仅在Java虚拟机中发生动态连接失败或其它的定位失败的时候抛出一个Error对象。一般程序...

checked exception和unchecked exception区别

一、 Java 中定义了两类异常: 1) Checked exception: 这类异常都是Exception的子类 。异常的向上抛出机制进行处理,如果子类可能产生A异常,那么在父类中也必须thr...

Checked vs UnChecked 异常

 Checked vs UnChecked 异常 ,使用场合?异常的概念          任何的异常都是Throwable类(为何不是接口??),并且在它之下包含两个字类Error / Except...

Java的checked和unchecked异常

Java异常分为两种类型,checked异常和unchecked异常,另一种叫法是异常和错误。 简单地讲,checked是可以在执行过程中恢复的,而unchecked异常则是无法处理的错误。 ch...

Java包含两种异常checked异常和unchecked异常

Java包含两种异常:checked异常和unchecked异常。C#只有unchecked异常。checked和unchecked异常之间的区别是: Checked异常必须被显式地捕获...

详解Java中的checked异常和unchecked异常

Java中checked和unchecked异常的区分,以及java的异常层次结构,应该对哪些异常进行声明。...

浅析java中的已检查(checked)异常和未检查(unchecked)异常

作为一名java程序员,如果不懂异常,就绝对不是一名合格的程序员。异常看似简单,但是真正理解设计这种机制的背后用意,不是每一个人都能够做到的。小弟不才,试着分析一下,希望能为初学者带来一点启发。如果有...

java中checked和unchecked 异常处理的例子

有两种类型的异常:一种是checked异常一种是unchecked异常,在这篇文章中我们将利用实例来学习这两种异常,checked的异常和unchecked异常最大的区别就是checked去唱是在编译...

Java异常处理原则,check and Unchecked Exception

异常的概念          任何的异常都是Throwable类(为何不是接口??),并且在它之下包含两个字类Error / Exception,而Error仅在当在Java虚拟机中发生动态连接...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Java - Checked vs Unchecked Exception
举报原因:
原因补充:

(最多只允许输入30个字)