服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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 )很容易(有时也很诱人),但如果一开始就建立一个可靠的、深思熟虑的异常模型,则您将在应用程序上少花很多精力。创建一个嵌套的、层次结构化的异常框架将改进代码的可读性及其可用性。

扫描关注微信公众号