`

Checked Exception与Runtime Exception 的区别(转)

阅读更多
原文:http://www.jspcn.net/htmlnews/11049321929371278.html

  Java里有个很重要的特色是Exception ,也就是说允许程序产生例外状况。而在学Java 的时候,我们也只知道Exception 的写法,却未必真能了解不同种类的Exception 的区别。

  首先,您应该知道的是Java 提供了两种Exception 的模式,一种是执行的时候所产生的Exception (Runtime Exception),另外一种则是受控制的Exception (Checked Exception)。

  所有的Checked Exception 均从java.lang.Exception 继承而来,而Runtime Exception 则继承java.lang.RuntimeException 或java.lang.Error (实际上java.lang.RuntimeException 的上一层也是java.lang.Exception)。

  当我们撰写程序的时候,我们很可能会对选择某种形式的Exception 感到困扰,到底我应该选择Runtime Exception 还是Checked Exception ?

  其实,在运作上,我们可以通过Class 的Method 如何产生某个Exception以及某个程序如何处理这个被产生来的Exception 来了解它们之间的差异。
首先我们先建立一个Exception

public class CException extends Exception
{
public CException() {}
public CException(String message)
{
super(message);
}
}

然后我们撰写一个可能产生 CException 的 Class

public class testException
{
public void method1() throws CException
{
throw new CException("Test Exception");
}

public void method2(String msg)
{
if(msg == null)
{
throw new NullPointerException("Message is null");
}
}

public void method3() throws CException
{
method1();
}

// 以下省略
// ...
}
  
在这三个method 中,我们看到了method1 和method2 的程序码内都会产生Exception,但method3 的程序码中(大括号内),并没产生Exception,但在method3 的定义中,暗示了这个method 可能产生CException。

  呼叫method1() 的程序,必须将method1() 包含在try 与catch 中,如:

public class runtest
{
// ....
public static void main(String argv[])
{
testException te = new testException();
try
{
te.method1();
}
catch(CException ce)
{
// ....
}
}
// ...
}

  虽然包含在try 与catch 中,并不表示这段程序码一定会收到CException,但它的用意在于提醒呼叫者,执行这个method 可能产生的意外,而使用者也必须要能针对这个意外做出相对应的处理方式。

  当使用者呼叫method2() 时,并不需要使用try 和catch 将程序码包起来,因为method2 的定义中,并没有throws 任何的Exception ,如:

public class runtest
{
// ....
public static void main(String argv[])
{

testException te = new testException();

// 不会产生 Exception
te.method2("Hello");

// 会产生 Exception
te.method2(null);
}
// ...
}


  程序在执行的时候,也不见得会真的产生NullPointerException ,这种Exception 叫做runtime exception 也有人称为unchecked exception ,产生Runtime Exception 的method (在这个范例中是method2) 并不需要在宣告method 的时候定义它将会产生哪一种Exception 。

  在testException 的method3() 中,我们看到了另外一种状况,也就是method3里呼叫了method1() ,但却没有将method1 包在try 和catch 之间。相反,在method3() 的定义中,它定义了CException,实际上就是如果method3 收到了CException ,它将不处理这个CException ,而将它往外丢。当然,由于method3 的定义中有throws CException ,因此呼叫method3 的程序码也需要有try catch 才行。

  因此从程序的运作机制上看,Runtime Exception与Checked Exception 不一样,然而从逻辑上看,Runtime Exception 与Checked Exception 在使用的目的上也不一样。

  一般而言,Checked Exception 表示这个Exception 必须要被处理,也就是说程序设计者应该已经知道可能会收到某个Exception(因为要try catch住) ,所以程序设计者应该能针对这些不同的Checked Exception 做出不同的处理。

  而Runtime Exception 通常会暗示着程序上的错误,这种错误会导致程序设计者无法处理,而造成程序无法继续执行下去。

看看下面的例子:

String message[] = {"message1", "message2","message3"};
System.out.println(message[3]);

  这段程序码在Compile 时并没问题,但在执行时则会出现ArrayIndexOutOfBoundException 的例外,在这种状况下,我们亦无法针对这个Runtime Exception 做出有意义的动作,这就像是我们呼叫了testException 中的method2 ,却引发了它的NullPointerException 一样,在这种状况下,我们必须对程序码进行修改,从而避免这个问题。

  因此,实际上我们应该也必须要去抓取所有的Checked Exception,同时最好能在这些Checked Exception 发生的时候做出相对应的处理,好让程序能面对不同的状况。

  然而对于Runtime Exception ,有些人建议将它catch 住,然后导向其它地方,让程序继续执行下去,这种作法并非不好,但它会让我们在某些测试工具下认为我们的程序码没有问题,因为我们将Runtime Exception "处理"掉了,事实却不然!譬如很多人的习惯是在程序的进入点后用个大大的try catch 包起来,如:

public class runtest1
{
public static void main(String argv[])
{
try
{
//...
}
catch(Exception e)
{
}
}
}

  在这种情况下,我们很可能会不知道发生了什么Exception 或是从哪一行发出的,因此在面对不同的Checked Exception时,我们可已分别去try catch它。而在测试阶段时,如果碰到Runtime Exception ,我们可以让它就这样发生,接着再去修改我们的程序码,让它避免Runtime Exception,否则,我们就应该仔细追究每一个Exception ,直到我们可以确定它不会有Runtime Exception 为止!

  对于Checked Exception 与Runtime Exception ,我想应该有不少人会有不同的观点,无论如何,程序先要能执行,这些Exception 才有机会产生。因此,我们可以把这些Exception 当成是Bug ,也可以当成是不同的状况(Checked Exception),或当成是帮助我们除错的工具(Runtime Exception),但前提是我们需要处理这些Exception ,如果不处理,那么问题或状况就会永远留在那里。
 
分享到:
评论

相关推荐

    详解Java中Checked Exception与Runtime Exception 的区别

    主要介绍了详解Java中Checked Exception与Runtime Exception 的区别的相关资料,这里提供实例帮助大家学习理解这部分内容,需要的朋友可以参考下

    Java高级程序设计:第8章-异常处理.pptx

    能够区分checked exception和 runtime exception 会使用 try-catch-finally 处理异常 方法声明异常 抛出异常 自定义异常类 语法错误, 运行期错误, 逻辑错误 语法错误: 没有遵循语法规则导致的错误。 运行期错误: ...

    Java精华(免费版)

    另外,method3()本身并不会抛出exception,可是它却声明会抛出CheckedException。在向你解释之前,让我们先来看看这个类的main()方法:   public static void main( String[] args )   {   ...

    JSTL详细标签库介绍

    target=_blank>关于runtime exception和checked exception</A> <LI><A title="Java 理论与实践: 关于异常的争论" href="http://www.jspcn.net/htmlnews/11453819700151449.html" target=_blank>Java 理论与...

    【09-异常处理】

    Checked异常与Runtime异常 •Java的异常被分为两大类:Checked异常和Runtime异常(运行时异常)。所有 RuntimeException类及其子类的实例被称为Runtime异常;不是RuntimeException类及其子类 的异常...

    Java反射封装库joor.zip

     }}// There are many checked exceptions that you are likely to ignore anyway catch (Exception ignore) { // ... or maybe just wrap in your preferred runtime exception: throw new RuntimeException(e);...

    CLR via C# 3rd Edition

    增加了对checked和unchecked代码、BigInterger类型以及C# 4.0 dynamic类型的讨论。 Chapter 6-Type and Member Basics 无新话题。 Chapter 7-Constants and Fields 无新话题。 Chapter 8-Methods 新增了扩展...

    CSharp 3.0 With the .NET Framework 3.5 Unleashed(english)

    checked and unchecked Statements 274 Summary 277 12 Event-Based Programming with Delegates and Events 278 Exposing Delegates 279 Implementing Delegate Inference 285 Assigning Anonymous ...

    Professional.MFC.with.VC6

    Invisible at Runtime Show in "Insert Object" Dialog About Box Simple Frame Subclassing a Control Protecting the Innocent What's in the Project? COleControl Properties Stock Properties Adding...

    c#学习笔记.txt

    Checked 和 Uncheckedchecked, unchecked fixed 语句Fixed lock 语句Lock (1) foreach 语句为数组或对象集合中的每个元素重复一个嵌入语句组。foreach 语句用于循环访问集合以获取所需信息,但不应用于更改集合内容...

    Professional C# 3rd Edition

    The Common Language Runtime 4 Advantages of Managed Code 4 A Closer Look at Intermediate Language 7 Support for Object Orientation and Interfaces 8 Distinct Value and Reference Types 9 Strong Data ...

    Thinking in Java 4th Edition

    Exception handling: dealing with errors ............... 31 Concurrent programming ... 32 Java and the Internet .......... 33 What is the Web? ......................... 33 Client-side programming ........

    C#浏览器编程,学习使用

    using System.Runtime.InteropServices; using SHDocVw; //******************************************************************// // 此类是一个主窗口类 // //本浏览器采用IE内核,多标签,多线程的一个浏览器,...

Global site tag (gtag.js) - Google Analytics