当前页面: 开发资料首页 → Java 专题 → 突破Java异常处理规则
突破Java异常处理规则
摘要: 我在我的应用程序中调用了外部方法并且想捕获它可能抛出的异常
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是不好的。捕获你能指定的异常对于代码的可维护性是十分重要的。然而这个规则依赖于特殊的环境。如果你不打算你的程序崩溃并且保留你的数据结构的安全异常,那么你必须捕获被抛出的真正的异常。
<table cellSpacing=0 cellPadding=0 width=365 align=center border=0>
<tr>
<td align=right></td><iframe align=right marginWidth=0 marginHeight=0 src="http://images.chinabyte.com/adjs/iframe-pip/y-software-pip.html" frameBorder=0 width=360 scrolling=no height=300></iframe></tr>
<tr>
<td><iframe src="http://www.my5757.com/tj/adbottom.htm" frameBorder=0 width=365 scrolling=no height=50></iframe></td></tr></table>
举个例子,想象你有一个加载了这个接口的服务器应用:
<table borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<tr>
<td>public interface IFoo
{
/**
* This method can't throw any checked exceptions...or can it?
*/
void bar ();
} // End of interface </td></tr></table>
对于给出参数的理由是让我们通知你这样的服务在什么地方,并且不同的IFoo实现能够从外部资源加载上。你写如下代码:
<table borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<tr>
<td>try
{
IFoo foo = ... // get an IFoo implementation
foo.bar ();
}
catch (RuntimeException ioe)
{
// Handle 'ioe' ...
}
catch (Error e)
{
// Handle or re-throw 'e' ...
} </td></tr></table>
并且你在这个里处理了所有可能的异常。你不需要在这里加上任何捕获java.io.IOException的异常,因为IFoo实现没有从IFoo.bar()中抛出它,对吗?(事实上,如果你加上了捕获java.io.IOException异常块,编译器可能会把它作为不可到达的异常而丢弃)
错误。在我写的EvilFoo类中bar()方法证明了将抛出你传递给类构造器的任何异常:
<table borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<tr>
<td>public void bar ()
{
EvilThrow.throwThrowable (m_throwthis);
} </td></tr></table>
运行Main方法:
<table borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<tr>
<td>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 </td></tr></table>
你将看到从bar()方法抛出的java.io.IOException异常实例并且没有任何捕获块:
<table borderColor=#ffcc66 width="90%" align=center bgColor=#e6e4dd border=1>
<tr>
<td>>java -cp classes Main
Exception in thread "main" java.io.IOException: SURPRISE!
at Main.main(Main.java:23) </td></tr></table>
在这里发生了什么?
↑返回目录
前一篇:
X3D实战基础讲座之十一
后一篇:
JDBC基础教程之连接