本文著重介紹了 java 異常選擇和使用中的一些誤區(qū),希望各位讀者能夠熟練掌握異常處理的一些注意點(diǎn)和原則,注意總結(jié)和歸納。只有處理好了異常,才能提升開發(fā)人員的基本素養(yǎng),提高系統(tǒng)的健壯性,提升用戶體驗(yàn),提高產(chǎn)品的價(jià)值。
誤區(qū)一、異常的選擇
圖 1. 異常分類
圖 1 描述了異常的結(jié)構(gòu),其實(shí)我們都知道異常分檢測(cè)異常和非檢測(cè)異常,但是在實(shí)際中又混淆了這兩種異常的應(yīng)用。由于非檢測(cè)異常使用方便,很多開發(fā)人員就認(rèn)為檢測(cè)異常沒什么用處。其實(shí)異常的應(yīng)用情景可以概括為以下:
一、調(diào)用代碼不能繼續(xù)執(zhí)行,需要立即終止。出現(xiàn)這種情況的可能性太多太多,例如服務(wù)器連接不上、參數(shù)不正確等。這些時(shí)候都適用非檢測(cè)異常,不需要調(diào)用代碼的顯式捕捉和處理,而且代碼簡(jiǎn)潔明了。
二、調(diào)用代碼需要進(jìn)一步處理和恢復(fù)。假如將 sqlexception 定義為非檢測(cè)異常,這樣操作數(shù)據(jù)時(shí)開發(fā)人員理所當(dāng)然的認(rèn)為 sqlexception 不需要調(diào)用代碼的顯式捕捉和處理,進(jìn)而會(huì)導(dǎo)致嚴(yán)重的 connection 不關(guān)閉、transaction 不回滾、db 中出現(xiàn)臟數(shù)據(jù)等情況,正因?yàn)?sqlexception 定義為檢測(cè)異常,才會(huì)驅(qū)使開發(fā)人員去顯式捕捉,并且在代碼產(chǎn)生異常后清理資源。當(dāng)然清理資源后,可以繼續(xù)拋出非檢測(cè)異常,阻止程序的執(zhí)行。根據(jù)觀察和理解,檢測(cè)異常大多可以應(yīng)用于工具類中。java學(xué)習(xí)群669823128
誤區(qū)二、將異常直接顯示在頁(yè)面或客戶端。
將異常直接打印在客戶端的例子屢見不鮮,以 jsp 為例,一旦代碼運(yùn)行出現(xiàn)異常,默認(rèn)情況下容器將異常堆棧信息直接打印在頁(yè)面上。其實(shí)從客戶角度來說,任何異常都沒有實(shí)際意義,絕大多數(shù)的客戶也根本看不懂異常信息,軟件開發(fā)也要盡量避免將異常直接呈現(xiàn)給用戶。
清單 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
<strong> package </strong> com.ibm.dw.sample.exception; /** * 自定義 runtimeexception * 添加錯(cuò)誤代碼屬性 */ <strong> public </strong> <strong> class </strong> <strong>runtimeexception</strong> <strong> extends </strong> <strong>java</strong>.<strong>lang</strong>.<strong>runtimeexception</strong> { //默認(rèn)錯(cuò)誤代碼 <strong> public </strong> <strong> static </strong> <strong> final </strong> integer generic = 1000000 ; //錯(cuò)誤代碼 <strong> private </strong> integer errorcode; <strong> public </strong> <strong>runtimeexception</strong>(integer errorcode, throwable cause) { <strong> this </strong>(errorcode, <strong> null </strong>, cause); } <strong> public </strong> <strong>runtimeexception</strong>(string message, throwable cause) { //利用通用錯(cuò)誤代碼 <strong> this </strong>(generic, message, cause); } <strong> public </strong> <strong>runtimeexception</strong>(integer errorcode, string message, throwable cause) { <strong> super </strong>(message, cause); <strong> this </strong>.errorcode = errorcode; } <strong> public </strong> integer <strong>geterrorcode</strong>() { <strong> return </strong> errorcode; } } |
正如示例代碼所示,在異常中引入錯(cuò)誤代碼,一旦出現(xiàn)異常,我們只要將異常的錯(cuò)誤代碼呈現(xiàn)給用戶,或者將錯(cuò)誤代碼轉(zhuǎn)換成更通俗易懂的提示。其實(shí)這里的錯(cuò)誤代碼還包含另外一個(gè)功能,開發(fā)人員亦可以根據(jù)錯(cuò)誤代碼準(zhǔn)確的知道了發(fā)生了什么類型異常。
誤區(qū)三、對(duì)代碼層次結(jié)構(gòu)的污染
我們經(jīng)常將代碼分 service、business logic、dao 等不同的層次結(jié)構(gòu),dao 層中會(huì)包含拋出異常的方法,如清單 2 所示:
清單 2
1
2
3
|
<strong> public </strong> customer <strong>retrievecustomerbyid</strong>( long id) <strong> throw </strong> sqlexception { //根據(jù) id 查詢數(shù)據(jù)庫(kù) } |
上面這段代碼咋一看沒什么問題,但是從設(shè)計(jì)耦合角度仔細(xì)考慮一下,這里的 sqlexception 污染到了上層調(diào)用代碼,調(diào)用層需要顯式的利用 try-catch 捕捉,或者向更上層次進(jìn)一步拋出。根據(jù)設(shè)計(jì)隔離原則,我們可以適當(dāng)修改成:
清單 3
1
2
3
4
5
6
7
8
9
10
|
<strong> public </strong> customer <strong>retrievecustomerbyid</strong>( long id) { <strong> try </strong>{ //根據(jù) id 查詢數(shù)據(jù)庫(kù) }<strong> catch </strong>(sqlexception e){ //利用非檢測(cè)異常封裝檢測(cè)異常,降低層次耦合 <strong> throw </strong> <strong> new </strong> runtimeexception(sqlerrorcode, e); }<strong> finally </strong>{ //關(guān)閉連接,清理資源 } } |
誤區(qū)四、忽略異常
如下異常處理只是將異常輸出到控制臺(tái),沒有任何意義。而且這里出現(xiàn)了異常并沒有中斷程序,進(jìn)而調(diào)用代碼繼續(xù)執(zhí)行,導(dǎo)致更多的異常。
清單 4
1
2
3
4
5
6
7
8
9
10
11
|
<strong> public </strong> <strong> void </strong> <strong>retrieveobjectbyid</strong>( long id){ <strong> try </strong>{ //..some code that throws sqlexception }<strong> catch </strong>(sqlexception ex){ /** *了解的人都知道,這里的異常打印毫無意義,僅僅是將錯(cuò)誤堆棧輸出到控制臺(tái)。 * 而在 production 環(huán)境中,需要將錯(cuò)誤堆棧輸出到日志。 * 而且這里 catch 處理之后程序繼續(xù)執(zhí)行,會(huì)導(dǎo)致進(jìn)一步的問題*/ ex.printstacktrace(); } } |
可以重構(gòu)成:
清單 5
1
2
3
4
5
6
7
8
9
10
11
|
<strong> public </strong> <strong> void </strong> <strong>retrieveobjectbyid</strong>( long id){ <strong> try </strong>{ //..some code that throws sqlexception } <strong> catch </strong>(sqlexception ex){ <strong> throw </strong> <strong> new </strong> runtimeexception(“exception <strong>in</strong> retieveobjectbyid”, ex); } <strong> finally </strong>{ //clean up resultset, statement, connection etc } } |
這個(gè)誤區(qū)比較基本,一般情況下都不會(huì)犯此低級(jí)錯(cuò)誤?。
誤區(qū)五、將異常包含在循環(huán)語句塊中
如下代碼所示,異常包含在 for 循環(huán)語句塊中。
清單 6
1
2
3
4
5
6
|
<strong> for </strong>(<strong> int </strong> i= 0 ; i< 100 ; i++){ <strong> try </strong>{ }<strong> catch </strong>(xxxexception e){ //…. } } |
我們都知道異常處理占用系統(tǒng)資源。一看,大家都認(rèn)為不會(huì)犯這樣的錯(cuò)誤。換個(gè)角度,類 a 中執(zhí)行了一段循環(huán),循環(huán)中調(diào)用了 b 類的方法,b 類中被調(diào)用的方法卻又包含 try-catch 這樣的語句塊。褪去類的層次結(jié)構(gòu),代碼和上面如出一轍。
誤區(qū)六、利用 exception 捕捉所有潛在的異常
一段方法執(zhí)行過程中拋出了幾個(gè)不同類型的異常,為了代碼簡(jiǎn)潔,利用基類 exception 捕捉所有潛在的異常,如下例所示:
清單 7
1
2
3
4
5
6
7
8
9
|
<strong> public </strong> <strong> void </strong> <strong>retrieveobjectbyid</strong>( long id){ <strong> try </strong>{ //…拋出 ioexception 的代碼調(diào)用 //…拋出 sqlexception 的代碼調(diào)用 }<strong> catch </strong>(exception e){ //這里利用基類 exception 捕捉的所有潛在的異常,如果多個(gè)層次這樣捕捉,會(huì)丟失原始異常的有效信息 <strong> throw </strong> <strong> new </strong> runtimeexception(“exception <strong>in</strong> retieveobjectbyid”, e); } } |
可以重構(gòu)成
清單 8
1
2
3
4
5
6
7
8
9
10
11
|
<strong> public </strong> <strong> void </strong> <strong>retrieveobjectbyid</strong>( long id){ <strong> try </strong>{ //..some code that throws runtimeexception, ioexception, sqlexception }<strong> catch </strong>(ioexception e){ //僅僅捕捉 ioexception <strong> throw </strong> <strong> new </strong> runtimeexception( /*指定這里 ioexception 對(duì)應(yīng)的錯(cuò)誤代碼*/code,“exception <strong>in</strong> retieveobjectbyid”, e); }<strong>catch</strong>(sqlexception e){ //僅僅捕捉 sqlexception <strong>throw</strong> <strong>new</strong> runtimeexception(/*指定這里 sqlexception 對(duì)應(yīng)的錯(cuò)誤代碼*/ code,“exception <strong>in</strong> retieveobjectbyid”, e); } } |
誤區(qū)七、多層次封裝拋出非檢測(cè)異常
如果我們一直堅(jiān)持不同類型的異常一定用不同的捕捉語句,那大部分例子可以繞過這一節(jié)了。但是如果僅僅一段代碼調(diào)用會(huì)拋出一種以上的異常時(shí),很多時(shí)候沒有必要每個(gè)不同類型的 exception 寫一段 catch 語句,對(duì)于開發(fā)來說,任何一種異常都足夠說明了程序的具體問題。
清單 9
1
2
3
4
5
6
7
|
<strong> try </strong>{ //可能拋出 runtimeexception、ioexeption 或者其它; //注意這里和誤區(qū)六的區(qū)別,這里是一段代碼拋出多種異常。以上是多段代碼,各自拋出不同的異常 }<strong> catch </strong>(<strong>exception</strong> e){ //一如既往的將 exception 轉(zhuǎn)換成 runtimeexception,但是這里的 e 其實(shí)是 runtimeexception 的實(shí)例,已經(jīng)在前段代碼中封裝過 <strong> throw </strong> <strong> new </strong> runtimeexception( /**/ code, /**/ , e); } |
如果我們?nèi)缟侠荆瑢⑺械?exception 再轉(zhuǎn)換成 runtimeexception,那么當(dāng) exception 的類型已經(jīng)是 runtimeexception 時(shí),我們又做了一次封裝。將 runtimeexception 又重新封裝了一次,進(jìn)而丟失了原有的 runtimeexception 攜帶的有效信息。
解決辦法是我們可以在 runtimeexception 類中添加相關(guān)的檢查,確認(rèn)參數(shù) throwable 不是 runtimeexception 的實(shí)例。如果是,將拷貝相應(yīng)的屬性到新建的實(shí)例上。或者用不同的 catch 語句塊捕捉 runtimeexception 和其它的 exception。個(gè)人偏好方式一,好處不言而喻。
誤區(qū)八、多層次打印異常
我們先看一下下面的例子,定義了 2 個(gè)類 a 和 b。其中 a 類中調(diào)用了 b 類的代碼,并且 a 類和 b 類中都捕捉打印了異常。
清單 10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<strong> public </strong> <strong> class </strong> <strong>a</strong> { <strong> private </strong> <strong> static </strong> logger logger = loggerfactory.getlogger(a. class ); <strong> public </strong> <strong> void </strong> <strong>process</strong>(){ <strong> try </strong>{ //實(shí)例化 b 類,可以換成其它注入等方式 b b = <strong> new </strong> b(); b.process(); //other code might cause exception } <strong> catch </strong>(xxxexception e){ //如果 b 類 process 方法拋出異常,異常會(huì)在 b 類中被打印,在這里也會(huì)被打印,從而會(huì)打印 2 次 logger.error(e); <strong> throw </strong> <strong> new </strong> runtimeexception( /* 錯(cuò)誤代碼 */ errorcode, /*異常信息*/msg, e); } } } <strong>public</strong> <strong>class</strong> <strong>b</strong>{ <strong>private</strong> <strong>static</strong> logger logger = loggerfactory.getlogger(b.class); <strong>public</strong> <strong>void</strong> <strong>process</strong>(){ <strong>try</strong>{ //可能拋出異常的代碼 } <strong>catch</strong>(xxxexception e){ logger.error(e); <strong>throw</strong> <strong>new</strong> runtimeexception(/* 錯(cuò)誤代碼 */ errorcode, /*異常信息*/ msg, e); } } } |
同一段異常會(huì)被打印 2 次。如果層次再?gòu)?fù)雜一點(diǎn),不去考慮打印日志消耗的系統(tǒng)性能,僅僅在異常日志中去定位異常具體的問題已經(jīng)夠頭疼的了。
其實(shí)打印日志只需要在代碼的最外層捕捉打印就可以了,異常打印也可以寫成 aop,織入到框架的最外層。
誤區(qū)九、異常包含的信息不能充分定位問題
異常不僅要能夠讓開發(fā)人員知道哪里出了問題,更多時(shí)候開發(fā)人員還需要知道是什么原因?qū)е碌膯栴},我們知道 java .lang.exception 有字符串類型參數(shù)的構(gòu)造方法,這個(gè)字符串可以自定義成通俗易懂的提示信息。
簡(jiǎn)單的自定義信息開發(fā)人員只能知道哪里出現(xiàn)了異常,但是很多的情況下,開發(fā)人員更需要知道是什么參數(shù)導(dǎo)致了這樣的異常。這個(gè)時(shí)候我們就需要將方法調(diào)用的參數(shù)信息追加到自定義信息中。下例只列舉了一個(gè)參數(shù)的情況,多個(gè)參數(shù)的情況下,可以單獨(dú)寫一個(gè)工具類組織這樣的字符串。
清單 11
1
2
3
4
5
6
7
8
|
public <strong> void </strong> retieveobjectbyid( long id){ <strong> try </strong>{ //..some code that throws sqlexception }<strong> catch </strong>(sqlexception ex){ //將參數(shù)信息添加到異常信息中 <strong> throw </strong> <strong> new </strong> runtimeexception(“exception <strong>in</strong> retieveobjectbyid <strong>with</strong> object id :”+ id, ex); } } |
誤區(qū)十、不能預(yù)知潛在的異常
在寫代碼的過程中,由于對(duì)調(diào)用代碼缺乏深層次的了解,不能準(zhǔn)確判斷是否調(diào)用的代碼會(huì)產(chǎn)生異常,因而忽略處理。在產(chǎn)生了 production bug 之后才想起來應(yīng)該在某段代碼處添加異常補(bǔ)捉,甚至不能準(zhǔn)確指出出現(xiàn)異常的原因。這就需要開發(fā)人員不僅知道自己在做什么,而且要去盡可能的知道別人做了什么,可能會(huì)導(dǎo)致什么結(jié)果,從全局去考慮整個(gè)應(yīng)用程序的處理過程。這些思想會(huì)影響我們對(duì)代碼的編寫和處理。
誤區(qū)十一、混用多種第三方日志庫(kù)
現(xiàn)如今 java 第三方日志庫(kù)的種類越來越多,一個(gè)大項(xiàng)目中會(huì)引入各種各樣的框架,而這些框架又會(huì)依賴不同的日志庫(kù)的實(shí)現(xiàn)。最麻煩的問題倒不是引入所有需要的這些日志庫(kù),問題在于引入的這些日志庫(kù)之間本身不兼容。如果在項(xiàng)目初期可能還好解決,可以把所有代碼中的日志庫(kù)根據(jù)需要重新引入一遍,或者換一套框架。但這樣的成本不是每個(gè)項(xiàng)目都承受的起的,而且越是隨著項(xiàng)目的進(jìn)行,這種風(fēng)險(xiǎn)就越大。
怎么樣才能有效的避免類似的問題發(fā)生呢,現(xiàn)在的大多數(shù)框架已經(jīng)考慮到了類似的問題,可以通過配置 properties 或 xml 文件、參數(shù)或者運(yùn)行時(shí)掃描 lib 庫(kù)中的日志實(shí)現(xiàn)類,真正在應(yīng)用程序運(yùn)行時(shí)才確定具體應(yīng)用哪個(gè)特定的日志庫(kù)。
其實(shí)根據(jù)不需要多層次打印日志那條原則,我們就可以簡(jiǎn)化很多原本調(diào)用日志打印代碼的類。很多情況下,我們可以利用攔截器或者過濾器實(shí)現(xiàn)日志的打印,降低代碼維護(hù)、遷移的成本。
結(jié)束語
以上純屬個(gè)人的經(jīng)驗(yàn)和總結(jié),事物都是辯證的,沒有絕對(duì)的原則,適合自己的才是最有效的原則。希望以上的講解和分析可以對(duì)您有所幫助。
原文鏈接:http://blog.csdn.net/garfielder007/article/details/52141737