(轉) C#中使用throw和throw ex拋出異常的區別

<div id="cnblogs_post_body" class="blogpost-body"><p><br>&nbsp;</p> <p>一般,咱們使用try/catch/finally語句塊來捕獲異常,就像在<a href="http://www.cnblogs.com/darrenji/p/3965443.html">這裏</a>說的。在拋出異常的時候,使用throw和throw ex有什麼區別呢?</p> <p><br>假設,按以下的方式調用幾個方法:</p> <p>&nbsp;</p> <p>→在Main方法中調用Method1方法,try/catch捕獲異常<br>→在Method1方法中調用Method2方法,try/catch捕獲異常<br>→在Method2方法中故意拋出異常,try/catch捕獲異常</p> <p>&nbsp;</p> <p><font color="#ff0000" size="5"><em>使用throw拋出異常</em></font></p> <p><br>&nbsp;</p><pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #f0f0f0"><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> static void Main(string[] args) </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> try </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> Method1(); </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> } </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> catch (Exception ex) </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> Console.WriteLine(ex.StackTrace.ToString()); </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> } </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"></pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> Console.ReadKey(); </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> } </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"></pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> static void Method1() </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> try </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> Method2(); </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> } </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> catch (Exception ex) </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> throw; </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> } </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> } </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"></pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> static void Method2() </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> try </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> throw new Exception("來自方法2的異常"); </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> } </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> catch (Exception ex) </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> throw; </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> } </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> } </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"></pre></pre> <p>&nbsp;</p> <p><a href="http://images0.cnblogs.com/blog/417212/201505/221133324006560.png"><img title="1" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="1" src="https://images0.cnblogs.com/blog/417212/201505/221133330251918.png" width="648" height="127"></a></p> <p>能夠看到,使用throw拋異常,把發生在Method2方法、Method1方法和Main方法中的異常所有拋了出來。</p> <p><br><font color="#ff0000" size="5"><em>使用throw ex拋出異常</em></font></p> <p><em><font color="#ff0000" size="5"></font></em>&nbsp;</p> <p>如今,在Method1方法中,使用throw ex拋出異常。</p> <p><br></p><pre style="overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; border-bottom: #cecece 1px solid; padding-bottom: 5px; padding-top: 5px; padding-left: 5px; min-height: 40px; border-left: #cecece 1px solid; padding-right: 5px; background-color: #f0f0f0"><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> static void Method1() </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> try </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> Method2(); </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> } </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> catch (Exception ex) </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> { </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> throw ex; </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> } </pre><pre style="font-size: 10px; font-family: consolas,'Courier New',courier,monospace; width: 100%; margin: 0em; background-color: #f0f0f0"> }</pre></pre><p></p> <p>&nbsp;</p> <p><a href="http://images0.cnblogs.com/blog/417212/201505/221133335724018.png"><img title="2" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="2" src="https://images0.cnblogs.com/blog/417212/201505/221133344007904.png" width="650" height="83"></a>&nbsp;&nbsp; </p> <p>&nbsp;</p> <p>可見, 使用throw ex拋異常,只會把Method1方法和Main方法中的異常拋出來。</p> <p>&nbsp;</p> <blockquote> <p><font color="#0000ff" size="5">總結:若是想獲取最完整的StackTrace信息,使用throw拋出異常,從中能夠知道異常到底來自哪一個方法。</font></p></blockquote></div>html

**轉自: C#中使用throw和throw ex拋出異常的區別 - Darren Ji - 博客園 http://www.cnblogs.com/darrenji/p/4521886.html **post

相關文章
相關標籤/搜索