title: Is an exception from a .NET method a terminating or not-terminating error?
body: <p>In this <a href="http://rkeithhill.wordpress.com/2009/08/03/effective-powershell-item-16-dealing-with-errors/" rel="nofollow noreferrer">great article</a> Keith explains the difference between terminating and non-terminating errors in Powershell. According to Keith exceptions thrown from calls to a member of a .NET object or type are non-terminating errors.</p>
  
  <p>Indeed if we define this .NET class for testing:</p>
  
  <pre><code>$a = Add-Type 'public class bla { public static void bl() { throw new System.ApplicationException("test"); }}' -PassThru
  </code></pre>
  
  <p>And then this function:</p>
  
  <pre><code>function tst { 1 | write-host; $a::bl(); 2 | Write-host }
  </code></pre>
  
  <p>we will see that when tst function is called the exception appears to be non-terminating: the second <code>Write-Host</code> works.</p>
  
  <p>But consider this:</p>
  
  <pre><code>function tst2 { try { tst } catch { "Catch!" } }
  </code></pre>
  
  <p>If we open <a href="http://technet.microsoft.com/en-us/library/hh847793.aspx" rel="nofollow noreferrer">the documentation</a>, we can read that the catch responds to or handles terminating errors in scripts. Throughout the text of the articles the errors the article is dealing with qualified as "terminating" in numerous places.</p>
  
  <p>So when we run the line above the second Write-Host does NOT run but the catch block does. It seems that our non-terminating error suddenly becomes terminating. </p>
  
  <p>How come?</p>
  
  <p>Another observation, is that with good old trap it's still non-terminating error:</p>
  
  <pre><code>function tst3 { tst trap { "Trap!" } }
  </code></pre>
  
  <p>Now, from practical perspective, what I want to achieve is the following. In a block of code I want to terminate on exceptions thrown from the .NET code. I want to leave terminating errors from cmdlets terminating and non-terminating errors from cmdlets non-terminating. </p>
  
  <p>How do I achieve this?</p>
  
  <p>Example:</p>
  
  <pre><code>Do-Something
  Call::Something()
  Do-SomethingElse
  Call::SomethingElse()
  Do-YetMoreSomething
  Call::YetMoreSomething()
  </code></pre>
  
  <p>I want to terminate on all the exceptions from .NET calls above. I also want to terminate on terminating errors from the cmdlets. I do not want to terminate on non-terminating errors from the cmdlets.</p>