Java开发框架之--异常框架设计._第1页
Java开发框架之--异常框架设计._第2页
免费预览已结束,剩余10页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

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

2、用(call stack运行时系统搜寻包含能够处理异常的代码块的方法所请求的堆栈。这个代码块 叫做异常处理器,搜寻首先从发生的方法开始,然后依次按着调用方法的倒序检索调 用堆栈。当找到一个相应的处理器时,运行时系统就把异常传递给这个处理器。一个异常处理器要适当地考滤抛出的异常对象的类型和异常处理器所处理的异常的类 型是否匹配。异常被捕获以后,异常处理器关闭。如果运行时系统搜寻了这个方法 的所有的调用堆栈,而没有找到相应的异常处理器。怎么设计异常框架任何的异常都是 Throwable 类(为何不是接口??并且在它之下包含两个字类Error / Exception,而 Error 仅在当在 Jav

3、a 虚拟机中发生动态连接失败或其它的定位失 败的时候 Java 虚拟机抛出一个 Error 对象。典型的简易程序不捕获或抛出Errors 对象,你可能永远不会遇到需要实例化 Error 的应用,那就让我们关心一下 ExceptionException中比较重要的就是 RuntimeException-运行时异常(当然这个名字是存 在争议的,因为任何的异常都只会发生在运行时,为什么说这个类时很重要的呢?因 为它直接关系到你的异常框架的设计,仔细看 RuntimeExceptionA method is not required to declare in its throws clause an

4、y subclasses ofRun timeExcepti on that might be throw n duri ng the executi on of the method but notcaught.-可能在执行方法期间抛出但未被捕获的Run timeException 的任何子类都无需在 throws 子句中进行声明。也就是说你的应用应该不去关心”说不关心是不服责任的,但只是你不应该试 图实例化它的字类 RuntimeException,就如同你不应该关心 Error 的产生与处理一样!RuntimeException 描述的是程序的错误引起来的,因该由程序负担这个责 任!(从

5、 责任这个角度看 Error 属于 JVM 需要负担的责任;RuntimeException 是程序应该负担 的责任;checked exception 是 具体应用负担的责任那就有人会问,那我该关心什么!答案就是除了 Error 与 RuntimeException,其他 剩下的异常都是你需要关心的,而这些异常类统称为 Checked Exception至于 Error 与 RuntimeExcepti on 则被统称为 Un checked Exceptio n.异常的概念就这些了,即使你在网络上搜索也就不过如此,是不是感觉到有点清 晰又有点模糊?那么怎么该如何在这样单薄而模糊的概念下设计

6、 J2EE 的异常框架 呢?解决方案:J2EE 异常框架我们拿一个模拟的例子来说明异常框架的设计过程,比如我们要对外提供doBus in ess 这个业务方法public void doBus in ess( throws xxxBus in essExceptio n当客户端调用这样的方法的时候应该这样处理异常(包括处理RuntimeException , checked exception记住,无论如何我们都不希望或者确切的说是不应该将Ru ntimeException 这样的异常暴露给客户的,因为他们没有解决这个问题的责任!我们暂时将 Struts 中的某个 Action 看作时客户端,

7、其中 doExecute(要调用doBus in ess 这个方法public void doAction(.tryxxx.doBus in ess(;catch(Exceptio n eif(e in sta nceof Run timeExcepti on/ catch run time excepti on/你可以在这里将捕获到的 Run timeException/将异常通知给某个负责此程序的程序员,让他知道他/自己犯了多么低级的错误else/checked excepti on such as xxxBus in essExcepti on/将这样的异常暴露给客户显示我们可以这样设计

8、 xxxBusinessExceptionpublic class xxxBus in essExcepti on exte nds Applicati on Excepti onpublic xxxBus in essExcepti on( Stri ng ssuper(s;import java.io.Pri ntStream;import java.io.Pri ntWriter;public class Applicati on Exceptio n exte nds Excepti on /* A wrapped Throwable */protected Throwable cau

9、se;public Applicatio nExcepti on( super(Error occurred in applicati on.;public ApplicationException(String message super(message;public Applicati on Excepti on( Stri ng message, Throwable cause super(message; this.cause = cause;/ Created to match the JDK 1.4 Throwable method.public Throwable in itCa

10、use(Throwable cause this.cause = cause;retur n cause;public String getMessage( / Get this excepti ons message.String msg = super.getMessage(;Throwable pare nt = this;Throwable child;/ Look for n ested excepti ons.while(child = getNestedExceptio n(pare nt != n ull / Get the childs message.String msg2

11、 = child.getMessage(;/If we found a message for the child excepti on, / we appe nd it.if (msg2 != null if (msg != null msg += : + msg2; else msg = msg2;/ Any nested ApplicationException will append its own / children, so we need tobreak out of here.if (child in sta nceof Applicati on Excepti on brea

12、k;pare nt = child;/ Retur n the completed message.return msg;public void prin tStackTrace( / Print the stack trace for this exception.super.pri ntStackTrace(;Throwable pare nt = this;Throwable child;/ Print the stack trace for each n ested excepti on. while(child =getNestedExceptio n(pare nt != n ul

13、l if (child != null System.err.pri nt(Caused by:;child.pri ntStackTrace(;if (child in sta nceof Applicati on Excepti on break;pare nt = child;public void prin tStackTrace(Pri ntStream s / Print the stack trace for this exception.super.pri ntStackTrace(s;Throwable pare nt = this;Throwable child;/ Pri

14、nt the stack trace for each n ested excepti on.while(child = getNestedExceptio n( pare nt != n ull if (child != n ull s.pri nt(Caused by:;child.pri ntStackTrace(s;if (child instanceof ApplicationException break;pare nt = child;public void prin tStackTrace(Pri ntWriter w / Print the stack trace for t

15、his exception.super.pri ntStackTrace(w;Throwable pare nt = this;/ Print the stack trace for each n ested excepti on.tryThrowable child;while(child = getNestedExceptio n( pare nt != n ull if (child != n ull w.pri nt(Caused by:;child.pri ntStackTrace(w;if (child instanceof ApplicationException break;p

16、are nt = child;public Throwable getCause( retur n cause;而聪明的读者肯定要问我那 doBus in ess 这个业务方法该如何包装异常呢?public void doBus in ess( throw xxxBus in essExceptio ntryexecute1(; /if it throw excepti on1exexute2(; / if it throw excepti on 2 catch (excepti on1 elthrow new xxxBus in essExcepti on(e1;catch(excepti

17、on2 e2throw new xxxBus in essExcepti on(e2;也可以这样public void doBus in ess( throw xxxBus in essExcepti onexecute1(; /if it throw excepti on1exexute2(; / if it throw excepti on 2catch (Excepti on e/注意很多应用在这里根本不判断异常的类型而一股脑的采用/ throw new xxxBus in essExcepti on(e;/ 而这样带来的问题就是 xxxBusinessException吞掉了 RuntimeException/ 从而将 checked excption 与 unchecked exception?昆在了一起!/其实 xxxBusinessException 属于 checked excpetion 它根本不应该也不能够理睬 RuntimeExceptionif(! e in sta nceof Run timeExcepti on throw new xxxBus in essExceptio n(e;总结1。JAVA 的异常分为两类:checked exception &am

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论