title: PowerShell Try/Catch with If Statements
body: <h3>Problem/Details</h3>
  
  <p>I am working in PowerShell and trying to figure out how custom Try Catch statements work. My current major issue involves mixing Try/Catch and If statements. So the idea of what I am trying to achieve is something like this:</p>
  
  <pre><code>try {
      if (!$someVariable.Text) { throw new exception 0 }
      elseif ($someVariable.Text -lt 11) { throw new exception 1 }
      elseif (!($someVariable.Text -match '[a-zA-Z\s]')) { throw new exception 2}
  }
  catch 0 {
      [System.Windows.Forms.MessageBox]::Show("Custom Error Message 1")
  }
  catch 1 {
      [System.Windows.Forms.MessageBox]::Show("Custom Error Message 2")
  }
  catch 2 {
      [System.Windows.Forms.MessageBox]::Show("Custom Error Message 3")
  }
  </code></pre>
  
  <p>Now I know the above code is very inaccurate in terms of what the actual code will be, but I wanted to visually display what I'm thinking and trying to achieve.</p>
  
  <h3>Question</h3>
  
  <p>Does anyone know how to create custom error messages with PowerShell that could assist me with achieving something close to the above idea and explain your answer a bit? Thank you in advance </p>
  
  <p>So far, the link below is the closest thing I have found to what I'm trying to achieve:</p>
  
  <p><a href="https://stackoverflow.com/questions/24970528/powershell-try-catch-custom-terminating-error-message">PowerShell Try, Catch, custom terminating error message</a></p>