Java開發(fā)框架之--異??蚣茉O(shè)計_第1頁
Java開發(fā)框架之--異??蚣茉O(shè)計_第2頁
Java開發(fā)框架之--異??蚣茉O(shè)計_第3頁
Java開發(fā)框架之--異常框架設(shè)計_第4頁
Java開發(fā)框架之--異??蚣茉O(shè)計_第5頁
已閱讀5頁,還剩7頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、概念什么是異常?異常(exception應該是異常事件(exceptional event的縮寫。異常定義:異常是一個在程序執(zhí)行期間發(fā)生的事件,它中斷正在執(zhí)行的程序的正常的指令流。當在一個方法中發(fā)生錯誤的時候,這個方法創(chuàng)建一個對象,并且把它傳遞給運行時系統(tǒng)。這個對象被叫做異常對象,它包 含了有關(guān)錯誤的信息,這些信息包括錯誤的類型和在程序發(fā)生錯誤時的狀態(tài)。創(chuàng)建一個錯誤對象并把它傳遞給運行時系統(tǒng) 被叫做拋出異常。一個方法拋出異常后,運行時系統(tǒng)就會試著查找一些方法來處理它。這些處理異常的可能的方法的集合是被整理在一起的 方法列表,這些方法能夠被發(fā)生錯誤的方法調(diào)用。這個方法列表被叫做堆棧調(diào)用(call

2、stack運行時系統(tǒng)搜尋包含能夠處理異常的代碼塊的方法所請求的堆棧。這個代碼塊叫做異常處理器,搜尋首先從發(fā)生的方法開 始,然后依次按著調(diào)用方法的倒序檢索調(diào)用堆棧。當找到一個相應的處理器時,運行時系統(tǒng)就把異常傳遞給這個處理器。 一個異常處理器要適當?shù)乜紴V拋出的異常對象的類型和異常處理器所處理的異常的類型是否匹配。異常被捕獲以后,異常 處理器關(guān)閉。如果運行時系統(tǒng)搜尋了這個方法的所有的調(diào)用堆棧,而沒有找到相應的異常處理器。怎么設(shè)計異??蚣苋魏蔚漠惓6际荰hrowable類(為何不是接口?,并且在它之下包含兩個字類Error / Exception,而Error僅在當在 Java虛擬機中發(fā)生動態(tài)連接失

3、敗或其它的定位失敗的時候,Java虛擬機拋出一個Error對象。典型的簡易程序不捕獲或拋出 Errors對象,你可能永遠不會遇到需要實例化Error的應用,那就讓我們關(guān)心一下ExceptionException中比較重要的就是RuntimeException-運行時異常(當然這個名字是存在爭議的,因為任何的異常都只會發(fā)生在 運行時,為什么說這個類時很重要的呢?因為它直接關(guān)系到你的異??蚣艿脑O(shè)計,仔細看RuntimeExceptionA method is not required to declare in its throws clause any subclasses of Runtime

4、Exception that might be thrown during the execution of the method but not caught.-可能在執(zhí)行方法期間拋出但未被捕獲的 RuntimeException 的任何子類都無需在 throws 子句中進行聲明。也就是說你的應用應該不去“關(guān)心”(說不關(guān)心是不服責任的,但只是你不應該試圖實例化它的字類RuntimeException ,就如同你不應該關(guān)心Error的產(chǎn)生與處理一樣!RuntimeException描述的是程序的錯誤引起來的,因該由程序負擔這個責 任!(從責任這個角度看Error屬于JVM需要負擔的責任;Run

5、timeException是程序應該負擔的責任;checked exception 是 具體應用負擔的責任那就有人會問,那我該關(guān)心什么!答案就是除了Error與RuntimeException,其他剩下的異常都是你需要關(guān)心的,而這些異 常類統(tǒng)稱為Checked Exception,至于Error與RuntimeException則被統(tǒng)稱為Unchecked Exception.異常的概念就這些了,即使你在網(wǎng)絡(luò)上搜索也就不過如此,是不是感覺到有點清晰又有點模糊?那么怎么該如何在這樣單 薄而模糊的概念下設(shè)計J2EE的異??蚣苣?解決方案:J2EE異??蚣芪覀兡靡粋€模擬的例子來說明異??蚣艿脑O(shè)計過程

6、,比如我們要對外提供doBusiness(這個業(yè)務(wù)方法public void doBusiness( throws xxxBusinessException當客戶端調(diào)用這樣的方法的時候應該這樣處理異常(包括處理RuntimeException , checked exception記住,無論如何我們都不希望或者確切的說是不應該將RuntimeException這樣的異常暴露給客戶的,因為他們沒有解決這 個問題的責任!我們暫時將Struts中的某個Action看作時客戶端,其中doExecute(.要調(diào)用doBusiness(這個方法public void doAction(.tryxxx.do

7、Business(;catch(Exception eif(e instanceof RuntimeException/ catch runtime exception/ 你可以在這里將捕獲到的RuntimeException/ 將異常通知給某個負責此程序的程序員,讓他知道他/ 自己犯了多么低級的錯誤!else/checked exception such as xxxBusinessException/將這樣的異常暴露給客戶顯示我們可以這樣設(shè)計xxxBusinessExceptionpublic class xxxBusinessException extends ApplicationEx

8、ceptionpublic xxxBusinessException(String ssuper(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 ApplicationExceptio

9、n(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 exceptions messa

10、ge.String msg = super.getMessage(;Throwable parent = this;Throwable child;/ Look for nested exceptions.while(child = getNestedException(parent != null / Get the childs message.String msg2 = child.getMessage(;/ If we found a message for the child exception, / we append it.if (msg2 != null if (msg !=

11、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 th

12、is 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 =

13、 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.pr

14、intStackTrace(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 = getNeste

15、dException(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(這個業(yè)務(wù)方法該如何包裝異常呢? public void doBusiness( throw xxxBusinessExceptiontryexecute1(; / if i

16、t throw exception1exexute2(; / if it throw exception 2. .catch (exception1 e1throw new xxxBusinessException(e1;catch(exception2 e2throw new xxxBusinessException(e2;.也可以這樣public void doBusiness( throw xxxBusinessExceptiontryexecute1(; / if it throw exception1exexute2(; / if it throw exception 2. .cat

17、ch (Exception e/ 注意很多應用在這里根本不判斷異常的類型而一股腦的采用/ throw new xxxBusinessException(e;/ 而這樣帶來的問題就是xxxBusinessException吞掉了RuntimeException/ 從而將checked excption 與unchecked exception混在了一起!/ 其實xxxBusinessException屬于checked excpetion ,它根本不應該也不能夠理睬RuntimeExceptionif(! e instanceof RuntimeException throw new xxxBusinessException(e;總結(jié)1。JAVA的異常分為兩類: checked excep

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論