title: Powershell code not exiting after try catch block
body: <p>I have a code that downloads a file from SharePoint, edits it, uploads it back to SharePoint and finally sends a confirmation email. I have functions for each of the tasks. The code works fine.</p>
  <p>However, I want to add error exception for a condition when if the file is open in SharePoint by some user, show error message and exit code. The issue I am experiencing is the code continues to run even if there is an exception. In the below code, the sendMail function is called even when the getSharePointFile function fails.</p>
  <p>I have tried $ErrorActionPreference = &quot;Stop&quot; but with that, the catch block is not executed and my custom error MessageBox is not displayed. Thanks in advance.</p>
  <p>Here is the relevant code:</p>
  <pre><code>function getSharePointFile {
      Connect-PnPOnline $SharepointURL -UseWebLogin
      Get-PnPFile -Url $fileRelativeURL -Path $localFilePath -FileName $fileName -AsFile -Force 
  }
  
  function runCatch {
      $showMessage = [System.Windows.Forms.MessageBox]::Show($_)
      exit
  }
  
  try {getSharePointFile}   
  catch{runCatch}
  
  try {updateAuditResults}   
  catch{runCatch}
  
  try {uploadToSharePoint}   
  catch{runCatch}
  
  try {sendMail}   
  catch{runCatch}
  
  </code></pre>