打印本文 打印本文 关闭窗口 关闭窗口
突破Java异常处理规则
作者:武汉SEO闵涛  文章来源:敏韬网  点击数827  更新时间:2009/4/22 23:28:40  文章录入:mintao  责任编辑:mintao
  Q: 我在我的应用程序中调用了外部方法并且想捕获它可能抛出的异常。我能捕获java.lang.Exception吗?

  A: 通过一个给定的方法去处理所有运行时和检测异常对于预防外部错误是不充分的。

  你可以去读目前 JavaWorld文章 – “Java Tip 134: When Catching Exception, Don’t Cast Your Net Too Wide”。这篇文章警告了捕获java.lang.Exception和java.lang.Throable是不好的。捕获你能指定的异常对于代码的可维护性是十分重要的。然而这个规则依赖于特殊的环境。如果你不打算你的程序崩溃并且保留你的数据结构的安全异常,那么你必须捕获被抛出的真正的异常。



  举个例子,想象你有一个加载了这个接口的服务器应用:

public interface IFoo
{
 /**
 * This method can't throw any checked exceptions...or can it?
 */
 void bar ();
} // End of interface
  对于给出参数的理由是让我们通知你这样的服务在什么地方,并且不同的IFoo实现能够从外部资源加载上。你写如下代码:

try
{
 IFoo foo = ... // get an IFoo implementation
 foo.bar ();
}
catch (RuntimeException ioe)
{
 // Handle 'ioe' ...
}
catch (Error e)
{
 // Handle or re-throw 'e' ...
}
  并且你在这个里处理了所有可能的异常。你不需要在这里加上任何捕获java.io.IOException的异常,因为IFoo实现没有从IFoo.bar()中抛出它,对吗?(事实上,如果你加上了捕获java.io.IOException异常块,编译器可能会把它作为不可到达的异常而丢弃)

  错误。在我写的EvilFoo类中bar()方法证明了将抛出你传递给类构造器的任何异常:

public void bar ()
{
 EvilThrow.throwThrowable (m_throwthis);
}
  运行Main方法:

public class Main
{
 public static void main (final String[] args)
 {
  // This try/catch block appears to intercept all exceptions that
  // IFoo.bar() can throw; however, this is not true
  try
  {
   IFoo foo = new EvilFoo (new java.io.IOException ("SURPRISE!"));
   foo.bar ();
  }
  catch (RuntimeException ioe)
  {
   // Ignore ioe
  }
  catch (Error e)
  {
   // Ignore e
  }
 }
} // End of class
  你将看到从bar()方法抛出的java.io.IOException异常实例并且没有任何捕获块:

>java -cp classes Main
Exception in thread "main" java.io.IOException: SURPRISE!
at Main.main(Main.java:23)
  在这里发生了什么?

打印本文 打印本文 关闭窗口 关闭窗口