title: PowerShell Error Handling with Functions
body: <p>This is a best practice question I suppose.</p>
  
  <p>When designing functions that will be used within scripts, what is the best way to handle errors that could occur within the function?</p>
  
  <p>For example, say we have a basic function that does X and Y:</p>
  
  <pre><code>Function Test-Function
  {
      Try
      {
          &lt;# Something in here that would generate an error #&gt;
      }
      Catch
      {
          Throw
      }
  
      Return $someReturnResultIWantInMyScript
  }
  </code></pre>
  
  <p>My script calls this function:</p>
  
  <pre><code>Try
  {
      $ValueIWantFromFunction = Test-Function
  }
  Catch
  {
      &lt;# Do something here #&gt;
  }
  </code></pre>
  
  <p>If the <code>Test-Function</code> hits a terminating error, it will throw to the caller. The <code>Try/Catch</code> around my function call in the script will receive this error and hit its own catch. I can then decide what to do.</p>
  
  <p>If I didn't throw an error within the function, the script wouldn't see the terminating error, and then my <code>$ValueIWantFromFunction</code> could contain <code>$Null</code> or something not useful.</p>
  
  <p>Is this a good way of error handling with functions and function calls within scripts?  Is there a better way? </p>