码迷,mamicode.com
首页 > 编程语言 > 详细

java中的Checked Exception和Unchecked Exception的区别

时间:2014-12-19 11:26:12      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   ar   io   sp   for   java   strong   

Java 定义了两种异常:

  - Checked exception: 继承自 Exception 类是 checked exception。代码需要处理 API 抛出的 checked exception,要么用 catch 语句,要么直接用 throws 语句抛出去。

  - Unchecked exception: 也称 RuntimeException,它也是继承自 Exception。但所有 RuntimeException 的子类都有个特点,就是代码不需要处理它们的异常也能通过编译,所以它们称作 unchecked exception。RuntimeException(运行时异常)不需要try...catch...或throws 机制去处理的异常。

NullpointerException 的继承级别。 

bubuko.com,布布扣 

NullpointerException 继承自 RuntimeException,所以它是个 unchecked exception。

 最常用的五种RuntimeException:    

 

 ArithmeticException

int a=0;
int b= 3/a;

 ClassCastException:

Object x = new Integer(0);
System.out.println((String)x);

 IndexOutOfBoundsException
    ArrayIndexOutOfBoundsException,
    StringIndexOutOfBoundsException 

int [] numbers = { 1, 2, 3 };
int sum = numbers[3];

IllegalArgumentException
    NumberFormatException

int a = Interger.parseInt("test");

NullPointerExceptionextends

 

 

小结:

检查性异常: 不处理编译不能通过

非检查性异常:不处理编译可以通过,如果有抛出直接抛到控制台。

运行时异常: 就是非检查性异常

非运行时异常: 就是检查性异常

 

java中的Checked Exception和Unchecked Exception的区别

标签:style   blog   http   ar   io   sp   for   java   strong   

原文地址:http://www.cnblogs.com/liuhongfeng/p/4173232.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!