Note that there are some explanatory texts on larger screens.

plurals
  1. POCan the (plain) throw statement in C# cause exceptions?
    primarykey
    data
    text
    <p><strong><em>Question:</strong> Can the plain <code>throw</code> statement in C# ever cause a new exception in itself?</em></p> <hr> <p>Note that I ask this question out of <em>curiosity</em>, not because I have any practical or real-world situation where it would matter much. Also note that my gut feeling and experience tell me that the answer is "No", but I'm looking to validate that answer somehow (see further down on sources I've tried so far).</p> <p>Here's some sample code to illustrate my question:</p> <pre><code>try { int x = 0, y = 1 / x; } catch (Exception outerException) { try { throw; } catch (Exception innerException) { // Q: Does this Assert ever fail?? System.Diagnostics.Debug.Assert(outerException.Equals(innerException)); } } </code></pre> <p>I'm wondering if there's any way at all to alter the circumstances such that the <code>Assert</code> will fail, without touching the inner <code>try/catch</code> block. </p> <p>What I've tried or was looking to try to answer this:</p> <ul> <li>Read the <a href="http://msdn.microsoft.com/en-us/library/1ah5wsex.aspx" rel="noreferrer">throw (C# Reference)</a> page on MSDN - no definitive answer;</li> <li>Checked part 5.3.3.11 of the <a href="http://msdn.microsoft.com/en-us/library/ms228593.aspx" rel="noreferrer">C# Language Specification</a> - which is probably the wrong place to look for this kind of info;</li> <li>Glossed through the exceptions that I could try to trigger on the throw statement. The OutOfMemoryException comes to mind, but is kind of hard to trigger at the time of the <code>throw</code>.</li> <li>Opened up ILDASM to check the generated code. I can see that <code>throw</code> translates to a <code>rethrow</code> instruction, but I'm lost where to look further to check if that statement can or cannot throw an exception.</li> </ul> <p>This is what ILDASM shows for the inner <code>try</code> bit:</p> <pre><code>.try { IL_000d: nop IL_000e: rethrow } // end .try </code></pre> <p>So, to summarize: can a throw statement (used to rethrow an exception) ever <em>cause</em> an exception itself?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload