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学习指南系列(Java快速入门)

这是Java学习指南系列课程的第1篇,介绍Java语言的入门语法,引领希望学习Java语言编程的初学者进入Java大门。本课程不需要其他语言作为基础,可以直接学习。课程从Java开发平台的下载和安装开始,从浅到深、从易到难,循序渐进地进行语法讲解。为了让学员更好的掌握Java语言,本课程配套在线的Java题库及答案解析。相比于其他语言,Java语言更科学、更容易掌握,快来和大家一起学习Java吧。
  • 2017年08月09日 15:30

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

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

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

Java中checked和unchecked异常的区分,以及java的异常层次结构,应该对哪些异常进行声明。...
  • qq_14982047
  • qq_14982047
  • 2016-03-27 00:21:32
  • 10710

Java 中的两种异常(Checked exceptions 和 Unchecked exceptions)

Java中定义了两种类型的异常 Checked exceptions:checked exceptions继承自Exception类,调用抛出这种异常API的客户端代码必须要处理导常,否则是不能通过编...
  • hzh_csdn
  • hzh_csdn
  • 2016-11-21 16:30:01
  • 730

checked exception 和 unchecked exception区别

什么是unchecked异常? 即RuntimeException(运行时异常) 不需要try...catch...或throws 机制去处理的异常 Java异常的基类为jav...
  • tuolaji8
  • tuolaji8
  • 2015-10-11 20:45:16
  • 809

Unchecked Exception和Checked Exception的区别

Unchecked Exception:值得是程序的逻辑错误,程序的疵瑕,并且运行的时候无法恢复。 包括Error以及RuntimeException及其子类,如:OutOfMemoryError,...
  • qq_32519097
  • qq_32519097
  • 2016-10-06 20:47:27
  • 541

java中的Checked Exception和Unchecked Exception的区别

Java 定义了两种异常:   - Checked exception: 继承自 Exception 类是 checked exception。代码需要处理 API 抛出的 checked ex...
  • xjbclz
  • xjbclz
  • 2016-11-13 20:40:29
  • 437

Checked 和 Unchecked Exceptions的不同

原文地址:http://www.beingjavaguys.com/2013/04/exception-handling-in-java-exception.html 一篇详细介绍Checked Ex...
  • flycct
  • flycct
  • 2015-06-14 16:58:54
  • 1065

异常处理 - Checked还是Unchecked

下文我摘译自Rod Johnson的"J2EE Design and Developement", 解答了我长久以来的一个疑问, 希望对大家有用.异常处理 - Checked还是UncheckedJa...
  • sweetsoft
  • sweetsoft
  • 2006-01-03 21:20:00
  • 5411

checked Exception和unchecked Exception的区别

(一)Java的异常层次结构   要想明白Java中checked Exception和unchecked Exception的区别,我们首先来看一下Java的异常层次结构。      这是...
  • wei009965
  • wei009965
  • 2017-03-05 00:03:38
  • 342
收藏助手
不良信息举报
您举报文章:Java - Checked vs Unchecked Exception
举报原因:
原因补充:

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