企业应用程序在构建时常常对异常处理关注甚少,这会造成对低级异常(如 java.rmi.remoteexception 和 javax.naming.namingexception)的过度依赖。在 ejb 最佳实践的这篇专栏文章中,brett mclaughlin 解释了为什么对异常处理投入一点关注就会给我们带来很大的帮助,并向您展示了两种简单技术,它们将帮助您正确地构建更健壮且有用的异常处理框架。
在本系列先前的技巧文章中,异常处理不属于讨论的核心范围之内。但是,您可能会发现一点,那就是我们一直都回避来自 web 层的低级异常。我们向客户机提供诸如 applicationexception 和 invaliddataexception 之类的异常,而没有让 web 层处理象 java.rmi.remoteexception 或 javax.naming.namingexception 这样的异常。
远程和命名异常是系统级异常,而应用程序和非法数据异常是业务级异常,因为它们提交更适用的业务信息。当决定抛出何种类型的异常时,您应该总是首先考虑将要处理所报告异常的层。web 层通常是由执行业务任务的最终用户驱动的,所以最好用它处理业务级异常。但是,在 ejb 层,您正在执行系统级任务,如使用 jndi 或数据库。尽管这些任务最终将被合并到业务逻辑中,但是最好用诸如 remoteexception 之类的系统级异常来表示它们。
理论上,您可以让所有 web 层方法预期处理和响应单个应用程序异常,正如我们在先前的一些示例中所做的一样。但这种方法不适用于长时间运行。让您的委派方法抛出更具体的异常,这是一个好得多的异常处理方案,从根本上讲,这对接收客户机更有用。在这篇技巧文章中,我们将讨论两种技术,它们将有助于您创建信息更丰富、更具体的异常,而不会生成大量不必要的代码。
嵌套的异常
在设计可靠的异常处理方案时,要考虑的第一件事情就是对所谓的低级或系统级异常进行抽象化。这些核心 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;
}
}
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.");
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器