你會重拋異常(re-throw Exception)不?

我一直以來就是這樣寫代碼:

01: public void ThrowFunction()
02: {
03:    //Initialize
04:    String errMessage = "Just sample throwing.";
05:    Boolean isSomeWrong = true;
06:    //do something
07:    if(isSomeWrong)
08:      throw new Exception(errMessage);
09: }
10:
11: public Object GetSomeReturn()
12: {
13:   Object ret = null; // return value.
14:   Object someObj = null; // local value.
15: 
16:   try
17:   {
18:      //Initialize
19:      ret = new Object();
20:      somObj = new Object();
21:      // do some thing 
22:      ThrowFunction();
23:   }
24:   catch(Exception e)
25:   {
26:      // Clean up return value.
27:      if(ret != null) { ret.Dispose(); ret = null; }
28:      throw e;
29:   }
30:   finally
31:   {
32:      if(somObj != null) { somObj.Dispose(); somObj = null; }
33:   }
34:   return ret;
35: }

怎麼看都好像沒問題,msdn也是這樣寫的,連Java都是這樣寫的。但在總是在上一層的
catch中發現少了些信息。最近搜索到一篇文章才發現我正正寫錯了2年。而且,文章提
到的書也頗有名氣。問題在哪?就在於"throw e"。這些寫打斷了錯誤棧(stack)鏈
(break the exception chain or exception stack trace)。如果你查看這個
Exception變量的StackTrace屬性:
Exception: System.Exception: "Just sample throwing."
at XXX.GetSomeReturn() in YourProgramPath/SourceCodeName.cs:line 18

是line:28。但是就丟失了關於ThrowFunction發生的事。實際上如果這不是你要的結
果,你只要把28行改爲'throw;'即可。如果你catch exception僅僅爲了clean-up。
還可以把24行改爲'catch(Exception)' 甚至'catch'。最懶惰的寫法就是:

01: public void ThrowFunction()
02: {
03:    //Initialize
04:    String errMessage = "Just sample throwing.";
05:    Boolean isSomeWrong = true;
06:    //do something
07:    if(isSomeWrong)
08:      throw new Exception(errMessage);
09: }
10:
11: public Object GetSomeReturn()
12: {
13:   Object ret = null; // return value.
14:   Object someObj = null; // local value.
15: 
16:   try
17:   {
18:      //Initialize
19:      ret = new Object();
20:      somObj = new Object();
21:      // do some thing 
22:      ThrowFunction();
23:   }
24:   catch
25:   {
26:      // Clean up return value.
27:      if(ret != null) { ret.Dispose(); ret = null; }
28:      throw;
29:   }
30:   finally
31:   {
32:      if(somObj != null) { somObj.Dispose(); somObj = null; }
33:   }
34:   return ret;
35: }

當然如果你想重新包裝你的異常,則:

01: public void ThrowFunction()
02: {
03:    //Initialize
04:    String errMessage = "Just sample throwing.";
05:    Boolean isSomeWrong = true;
06:    //do something
07:    if(isSomeWrong)
08:      throw new Exception(errMessage);
09: }
10:
11: public Object GetSomeReturn()
12: {
13:   Object ret = null; // return value.
14:   Object someObj = null; // local value.
15: 
16:   try
17:   {
18:      //Initialize
19:      ret = new Object();
20:      somObj = new Object();
21:      // do some thing 
22:      ThrowFunction();
23:   }
24:   catch(Exception e)
25:   {
26:      // Clean up return value.
27:      if(ret != null) { ret.Dispose(); ret = null; }
28:      throw new YourException("Your costum error message",e);
29:   }
30:   finally
31:   {
32:      if(somObj != null) { somObj.Dispose(); somObj = null; }
33:   }
34:   return ret;
35: }

上面的寫法:
throw new YourException("Your costum error message",e);
同樣不會打斷stack trace。比如需要Log下異常然後重拋該異常而不打斷stack trace。
可以這樣寫:
01: public void ThrowFunction()
02: {
03:    //Initialize
04:    String errMessage = "Just sample throwing.";
05:    Boolean isSomeWrong = true;
06:    //do something
07:    if(isSomeWrong)
08:      throw new Exception(errMessage);
09: }
10:
11: public Object GetSomeReturn()
12: {
13:   Object ret = null; // return value.
14:   Object someObj = null; // local value.
15: 
16:   try
17:   {
18:      //Initialize
19:      ret = new Object();
20:      somObj = new Object();
21:      // do some thing 
22:      ThrowFunction();
23:   }
24:   catch(Exception e)
25:   {
26:      // Clean up return value.
27:      if(ret != null) { ret.Dispose(); ret = null; }
28:      YourLogClass.LogException(e);
29:      throw;
30:   }
31:   finally
32:   {
33:      if(somObj != null) { somObj.Dispose(); somObj = null; }
34:   }
35:   return ret;
25: }

Link
Read the original article about re-throw exception.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章