中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Java > 综合文章
EJB最佳实践:构建更好的异常处理框架
作者:Brett McLaughlin 时间:2006-09-23 10:14 出处:IBM dW 责编:月夜寒箫
              摘要:EJB最佳实践:构建更好的异常处理框架
嵌套的异常

在设计可靠的异常处理方案时,要考虑的第一件事情就是对所谓的 低级或 系统级异常进行抽象化。这些核心 Java 异常通常会报告网络流量中的错误、JNDI 或 RMI 问题,或者是应用程序中的其它技术问题。 RemoteException 、 EJBException 和 NamingException 是企业 Java 编程中低级异常的常见例子。

这些异常完全没有任何意义,由 Web 层的客户机接收时尤其容易混淆。如果客户机调用 purchase() 并接收到 NamingException ,那么它在解决这个异常时会一筹莫展。同时,应用程序代码可能需要访问这些异常中的信息,因此不能轻易地抛弃或忽略它们。

答案是提供一类更有用的异常,它还包含低级异常。清单 1 演示了一个专为这一点设计的简单 ApplicationException :

清单 1. 嵌套的异常

package com.ibm;
            import java.io.PrintStream;
            import java.io.PrintWriter;
            public class ApplicationException
            extends Exception
            {
            /** A wrapped Throwable */
            protected Throwable cause;
            public ApplicationException()
            {
            super("Error occurred in application.");
            }
            public ApplicationException
            (String message)
            {
            super(message);
            }
            public ApplicationException
            (String message, Throwable cause)
            {
            super(message);
            this.cause = cause;
            }
            // Created to match the
            JDK 1.4 Throwable method.
            public Throwable
            initCause(Throwable cause)
            {
            this.cause = cause;
            return cause;
            }
            public String getMessage()
            {
            // Get this exception's message.
            String msg = super.getMessage();
            Throwable parent = this;
            Throwable child;
            // Look for nested exceptions.
            while((child =
            getNestedException(parent)) != null)
            {
            // Get the child's message.
            String msg2 = child.getMessage();
            // If we found a message
            for the child exception,
            // we append it.
            if (msg2 != null)
            {
            if (msg != null)
            {
            msg += ": " + msg2;
            }
            else
            {
            msg = msg2;
            }
            }
            // Any nested ApplicationException
            will append its own
            // children, so we need to
            break out of here.
            if (child instanceof
            ApplicationException)
            {
            break;
            }
            parent = child;
            }
            // Return the completed message.
            return msg;
            }
            public void printStackTrace()
            {
            // Print the stack trace
            for this exception.
            super.printStackTrace();
            Throwable parent = this;
            Throwable child;
            // Print the stack trace for
            each nested exception.
            while((child = getNestedException
            (parent)) != null)
            {
            if (child != null)
            {
            System.err.print("Caused by: ");
            child.printStackTrace();
            if (child instanceof ApplicationException)
            {
            break;
            }
            parent = child;
            }
            }
            }
            public void printStackTrace(PrintStream s)
            {
            // Print the stack trace for this exception.
            super.printStackTrace(s);
            Throwable parent = this;
            Throwable child;
            // Print the stack trace
            for each nested exception.
            while((child = getNestedException
            (parent)) != null)
            {
            if (child != null)
            {
            s.print("Caused by: ");
            child.printStackTrace(s);
            if (child instanceof
            ApplicationException)
            {
            break;
            }
            parent = child;
            }
            }
            }
            public void printStackTrace(PrintWriter w)
            {
            // Print the stack trace
            for this exception.
            super.printStackTrace(w);
            Throwable parent = this;
            Throwable child;
            // Print the stack trace for
            each nested exception.
            while((child =
            getNestedException(parent)) != null)
            {
            if (child != null)
            {
            w.print("Caused by: ");
            child.printStackTrace(w);
            if (child instanceof
            ApplicationException)
            {
            break;
            }
            parent = child;
            }
            }
            }
            public Throwable getCause()
            {
            return cause;
            }
            }

清单1中的代码很简单;我们已经简单地将多个异常“串”在一起,以创建单个、嵌套的异常。但是,真正的好处在于将这种技术作为出发点,以创建特定于应用程序的异常层次结构。异常层次结构将允许 EJB 客户机既接收特定于业务的异常也接收特定于系统的信息,而不需要编写大量额外代码。

异常层次结构

异常层次结构应该从一些十分健壮而又通用的异常入手,如 ApplicationException 。如果您将顶级异常搞得太具体,那么其结果是您今后将不得不重新构造层次结构,以适应某些较通用的情况。

因此,让我们假定您的应用程序要求 NoSuchBookException 、 InsufficientFundsException 和 SystemUnavailableException 。您不必创建这三个异常,让它们继承 ApplicationException ,然后只需提供极少几个必须的构造器来创建格式化的消息。清单 2 是此类异常层次结构的示例:

清单 2. 异常层次结构

 

package com.ibm.library;
            import com.ibm.ApplicationException;
            public class NoSuchBookException
            extends ApplicationException
            {
            public NoSuchBookException
            (String bookName, String libraryName)
            {
            super("The book '" + bookName
            + "' was not found in the '" +
            libraryName + "' library.");
            }
            }

当需要编写大量专用异常时,异常层次结构极大地简化了工作。对于一个异常,为每个异常类添加一个或两个构造器,所花费时间很少不超过几分钟。您还经常需要给这些更具体的异常(这些异常也是主应用程序异常的子类)提供子类,以提供更具体的异常。例如,您可能需要 InvalidTitleException 和 BackorderedException 来继承 NoSuchBookException 。

企业应用程序在构建时通常都不会注意异常处理。尽管依靠低级异常(如 RemoteException 和 NamingException )很容易(有时也很诱人),但如果一开始就建立一个可靠的、深思熟虑的异常模型,则您将在应用程序上少花很多精力。创建一个嵌套的、层次结构化的异常框架将改进代码的可读性及其可用性。

关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有