Checked 和 Unchecked Exceptions的不同

来源:互联网 发布:会声会影x6软件下载 编辑:程序博客网 时间:2024/06/05 02:12
原文地址:http://www.beingjavaguys.com/2013/04/exception-handling-in-java-exception.html

什么是Exception?

Exception是在程序运行时发生的意外情况和事件, 如果不正确处理可能导致程序的崩溃。

Java中的Exceptions?

每当Java代码运行时发生错误,便会创建一个Exception对象。它持有Exception的所有信息,包括种类、类型和发生异常的位置。

Java运行时环境会为这个Exception对象寻找合适的处理程序。寻找顺序为:从当前方法开始,往上层的调用方法进行搜索,直到最后的Main方法。如果没有找到合适的处理代码,程序将会停止。

事件处理机制为程序提供代码片段,以便捕获Exception,保持程序的执行流。

Java中的Exception层次结构

Throwable是所有异常的父类,并且进一步扩展为Exception和Error两个类。Java中的Exceptions可以分为两个主要的分支



1) Unchecked Exception
2) Checked Exception

1) Unchecked Exception

unchecked exceptions在编译时不进行检查。继承自RuntimeException的类,便是unchecked exceptions

下面是一些unchecked exceptions的例子

ArithmeticException

正常条件下不被允许的数学运算:将0作为除数

package com.beingjavaguys.core;    public class ExceptionTest {    public static void main(String[] args) {  int i = 10/0;  }  } 

Console :

Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.beingjavaguys.core.ExceptionTest.main(ExceptionTest.java:6)

ArrayIndexOutOfBoundsException

试图访问不存在的索引,或者往错误的索引中插入值的时候会导致ArrayIndesOutOfBoundException

package com.beingjavaguys.core  public class ExceptionTest {    public static void main(String[] args) {  int arr[] = {'0','1','2'};  System.out.println(arr[4]);  }  }  

Console :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at com.beingjavaguys.core.ExceptionTest.main(ExceptionTest.java:7)

NullPointerException

试图访问一个null对象,或者对一个null值的对象进行操作

package com.beingjavaguys.core;  import java.util.ArrayList;    public class ExceptionTest {    public static void main(String[] args) {  String string = null;  System.out.println(string.length());  }  }  

Console

Exception in thread "main" java.lang.NullPointerException
at com.beingjavaguys.core.ExceptionTest.main(ExceptionTest.java:9)

一些Java中的Unchecked Exceptions

ArithmeticException
ArrayStoreException
BufferOverflowException
BufferUnderflowException
CannotRedoException
CannotUndoException
ClassCastException
CMMException
ConcurrentModificationException
DOMException
EmptyStackException
IllegalArgumentException
IllegalMonitorStateException
IllegalPathStateException
IllegalStateException
ImagingOpException
IndexOutOfBoundsException
MissingResourceException
NegativeArraySizeException
NoSuchElementException
NullPointerException
ProfileDataException
ProviderException
RasterFormatException
SecurityException
SystemException
UndeclaredThrowableException
UnmodifiableSetException
UnsupportedOperationException

2) CheckedExceptions

在编译时进行检查的异常称为checked exceptions。继承Exception的类,除了UncheckedException以外都是checked exceptions

在某些状况下,java编译器强制开发者在编译时编写异常处理程序,这种情况下抛出的异常称为Checked Exception,请看示例

try {      String input = reader.readLine();      System.out.println("You typed : "+input); // Exception prone area      } catch (IOException e) {        e.printStackTrace();  }  

从文件或者控制台进行读写操作的时候,名为IOException的checked Exception被抛出,这些exceptions在编译时被检查。开发者被强制要求编写异常处理程序。这就是为什么把这些异常称为Checked Exceptions

Java中的一些CheckedExceptions

FileNotFoundException
ParseException
ClassNotFoundException
CloneNotSupportedException
InstantiationException
InterruptedException
NoSuchMethodException
NoSuchFieldException






1 0