title: Powershell Try Catch in higher-order functions
body: <p>I'm trying to catch Exception calling a function running another function like this:</p>
  
  <pre><code>$ErrorActionPreference = "Stop"
  function f {
      $a = 1
      $b = $a / 0
  }
  function Main($f) {
      try {
          $f
      } catch [System.Exception] {
          "Caught exception"
      }
  }
  Main(f)
  </code></pre>
  
  <p>The problem is that the Exception is not caught and powershell show message like this:</p>
  
  <pre><code>Attempted to divide by zero.
  In C:\test.ps1:4 car:5
  +     $b = $a / 0
  +     ~~~~~~~~~~~
      + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
      + FullyQualifiedErrorId : RuntimeException
  </code></pre>
  
  <p>Why the exception is not caught even if <code>$ErrorActionPreference = "Stop"</code> is on the top of code?</p>