(转) 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

相关文章
相关标签/搜索