服务热线:13616026886

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

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

jsp/servlet/jsf--java异常框架设计

什么是异常?

异常(exception)应该是异常事件(exceptional event)的缩写。
异常定义:异常是一个在程序执行期间发生的事件,它中断正在执行的程序的正常的指令流。
当在一个方法中发生错误的时候,这个方法创建一个对象,并且把它传递给运行时系统。这个对象被叫做异常对象,它包含了有关错误的信息,这些信息包括错误的类型和在程序发生错误时的状态。创建一个错误对象并把它传递给运行时系统被叫做抛出异常。
一个方法抛出异常后,运行时系统就会试着查找一些方法来处理它。这些处理异常的可能的方法的集合是被整理在一起的方法列表,这些方法能够被发生错误的方法调用。这个方法列表被叫做堆栈调用(call stack)

运行时系统搜寻包含能够处理异常的代码块的方法所请求的堆栈。这个代码块叫做异常处理器,搜寻首先从发生的方法开始,然后依次按着调用方法的倒序检索调用堆栈。当找到一个相应的处理器时,运行时系统就把异常传递给这个处理器。一个异常处理器要适当地考滤抛出的异常对象的类型和异常处理器所处理的异常的类型是否匹配。异常被捕获以后,异常处理器关闭。如果运行时系统搜寻了这个方法的所有的调用堆栈,而没有找到相应的异常处理器。

 

怎么设计异常框架

任何的异常都是throwable类(为何不是接口??),并且在它之下包含两个字类error / exception,而error仅在当在java虚拟机中发生动态连接失败或其它的定位失败的时候,java虚拟机抛出一个error对象。典型的简易程序不捕获或抛出errors对象,你可能永远不会遇到需要实例化error的应用,那就让我们关心一下exception

exception中比较重要的就是runtimeexception-运行时异常(当然这个名字是存在争议的,因为任何的异常都只会发生在运行时),为什么说这个类时很重要的呢?因为它直接关系到你的异常框架的设计,仔细看runtimeexception

a method is not required to declare in its throws clause any subclasses of runtimeexception that might be thrown during the execution of the method but not caught.

-可能在执行方法期间抛出但未被捕获的 runtimeexception 的任何子类都无需在 throws 子句中进行声明。 

也就是说你的应用应该不去“关心”(说不关心是不服责任的,但只是你不应该试图实例化它的字类)runtimeexception,就如同你不应该关心error的产生与处理一样!runtimeexception描述的是程序的错误引起来的,因该由程序负担这个责任!(从责任这个角度看error属于jvm需要负担的责任;runtimeexception是程序应该负担的责任;checked exception 是具体应用负担的责任

那就有人会问,那我该关心什么!答案就是除了error与runtimeexception,其他剩下的异常都是你需要关心的,而这些异常类统称为checked exception,至于error与runtimeexception则被统称为unchecked exception.


异常的概念就这些了,即使你在网络上搜索也就不过如此,是不是感觉到有点清晰又有点模糊?那么怎么该如何在这样单薄而模糊的概念下设计j2ee的异常框架呢?


解决方案:j2ee异常框架

我们拿一个模拟的例子来说明异常框架的设计过程,比如我们要对外提供dobusiness()这个业务方法

public void dobusiness() throws xxxbusinessexception

当客户端调用这样的方法的时候应该这样处理异常(包括处理runtimeexception , checked exception)
记住,无论如何我们都不希望或者确切的说是不应该将runtimeexception这样的异常暴露给客户的,因为他们没有解决这个问题的责任!
我们暂时将struts中的某个action看作时客户端,其中doexecute(....)要调用dobusiness()这个方法

public void doaction(......)
{
 try
 {

  xxx.dobusiness();
 }
 catch(exception e)
 {
   if(e instanceof runtimeexception)  
   {
    // catch runtime exception
    // 你可以在这里将捕获到的runtimeexception
    // 将异常通知给某个负责此程序的程序员,让他知道他
    // 自己犯了多么低级的错误!


   }else
   {
    //checked exception such as xxxbusinessexception
    //将这样的异常暴露给客户显示    

   }

 }
}

我们可以这样设计xxxbusinessexception

public class xxxbusinessexception extends applicationexception 
{
    public xxxbusinessexception(string s){
        super(s);

};

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;
       }
}

而"聪明"的读者肯定要问我那dobusiness()这个业务方法该如何包装异常呢?

 public void dobusiness() throw xxxbusinessexception
 {
   try
   {
     execute1(); // if it throw exception1

     exexute2(); // if it throw exception 2

     .... ..... 

   }
   catch (exception1 e1)
   {
    throw new xxxbusinessexception(e1);
   }
   catch(exception2 e2)
   {
    throw new xxxbusinessexception(e2);
   }
   ........
 }

 也可以这样

 public void dobusiness() throw xxxbusinessexception
 {
   try
   {
     execute1(); // if it throw exception1

     exexute2(); // if it throw exception 2

     .... ..... 

   }
   catch (exception e)
   {
    // 注意很多应用在这里根本不判断异常的类型而一股脑的采用
    // throw new xxxbusinessexception(e);
    // 而这样带来的问题就是xxxbusinessexception"吞掉了"runtimeexception
    // 从而将checked excption 与unchecked exception混在了一起!

    // 其实xxxbusinessexception属于checked excpetion ,它根本不应该也不能够理睬runtimeexception
    if(! e instanceof runtimeexception) throw new xxxbusinessexception(e);
   }
 }


总结
 1。java的异常分为两类: checked exception & unchecked excpetion
 2。应用开发中产生的异常都应该集成自exception 但都属于checked excpetion类型
 3。应用中的每一层在包装并传递异常的时候要过滤掉runtimeexception!
 4。从责任这个角度看error属于jvm需要负担的责任;runtimeexception是程序应该负担的责任;checked exception 是具体应用负担的责任
 5。无论如何我们都不希望或者确切的说是不应该将runtimeexception这样的异常暴露给客户的,因为他们没有解决这个问题的责任!