title: Powershell trap [Exception] is not catching my error
body: <p>For some reason my script is not catching the exception when I run the following script against a file that does not exist. I based this code from examples I found on the web but it doesn't seem to work for me.</p>
  
  <p>I would appreciate any tips or pointers on how to fix this.</p>
  
  <p>Note: in the example below I've also tried</p>
  
  <pre><code>trap [Exception] {
  </code></pre>
  
  <p>but that didn't work either.</p>
  
  <p>Here's the script:</p>
  
  <pre><code>function CheckFile($f) {
  
        trap {
          write-host "file not found, skipping".
          continue
        }
  
        $modtime = (Get-ItemProperty $f).LastWriteTime
  
        write-host "if file not found then shouldn't see this"
  }
  
  
  write-host "checking a file that does not exist"
  CheckFile("C:\NotAFile")
  write-host "done."
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>PS &gt; .\testexception.ps1
  checking a file that does not exist
  Get-ItemProperty : Cannot find path 'C:\NotAFile' because it does not exist.
  At C:\Users\dleclair\Documents\Visual Studio 2010\lib\testexception.ps1:12 char:35
  +       $modtime = (Get-ItemProperty &lt;&lt;&lt;&lt;  $f).LastWriteTime
      + CategoryInfo          : ObjectNotFound: (C:\NotAFile:String) [Get-ItemProperty], ItemNotFoundException
      + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetItemPropertyCommand
  
  if file not found then shouldn't see this
  done.
  PS &gt;
  </code></pre>