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

來源:互聯網
上載者:User
重拋異常(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.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.