locked
PS Script continues past terminating-error ‘Throw’ with ‘ErrorActionPreference = 0’? RRS feed

  • Question

  • Hello,

     

    I was hoping someone could help me understand a bit more about PowerShell terminating and non-terminating errors. Because the below behavior means I think I have misunderstood

    I am using PS 5.1 on Windows 10.

    I was always taught that a PS terminating error would stop the PS script dead and these had to be dealt with Try-Catch blocks. To do this you had to set the ErrorActionPreference = Stop. I was also taught that calling throw is a terminating error.

    However, as I had to recently code a script which needed to continue if many errors were encountered I have been experimenting with ErrorActionPreference = 0 (silently continue). I was taught the ErrorActionPerference = 0 would only apply to non-terminating errors.

    As part of my testing I tried the following

    $Error.clear()
    $ErrorActionPreference = 0
    
    #Will produce an error using DSACLS.EXE CMD – as long as DSACLS (AD MANAGMENT TOOL FEATURE) installed and domain not called ‘adomain’
    $DsaclErr = DSACLS "DC=adomain,DC=com" /I:T /G "USER:SD" | Out-String
    #$? is result of last command error=0 true 
    if ($? -eq $false) {Throw $DsaclErr # @@DSACLS_ERR (-1 LINE)
    }
    
    # plus syntax error
    sfsfs
    
    #These seem to continue even though throw is terminating?
    $Error.invocationinfo.ScriptLineNumber 
    $Error

    My question is:

    1. Why does the script continue past the Throw method and display ($Error.invocationinfo.ScriptLineNumber) if Throw is a terminating error?

    Just to be clear this behavior is not bad for me actually I quite like it. I just want more info about why this behavior occurs has PS behavior changed through different versions for example? If its a consistent reason I can use $ErrorActionPreference = 0’ more often and not worry about non-terminating\terminating errors in more PS scripts.

     

    I prefer to use throw as opposed to Write-error as this error object contains PS line information.

     



    Friday, June 29, 2018 4:04 PM

Answers

  • Throw does not generate a terminating error in that it does not stop the script.  A terminating error stops a single command.


    \_(ツ)_/

    • Marked as answer by PeteMitch99 Friday, June 29, 2018 4:53 PM
    Friday, June 29, 2018 4:22 PM

All replies

  • Without a Try/Catch you will not be able to stop the code.

    Example:

    Try{
        1/0
    }
    Catch{
       'error'
    }

    Also "$ErrorPreference=0" means "ignore all errors".


    \_(ツ)_/

    Friday, June 29, 2018 4:20 PM
  • Throw does not generate a terminating error in that it does not stop the script.  A terminating error stops a single command.


    \_(ツ)_/

    • Marked as answer by PeteMitch99 Friday, June 29, 2018 4:53 PM
    Friday, June 29, 2018 4:22 PM
  • Thanks for the quick replies.

    1) I see, so it stops all activity the line - but then continues the next line....?

    2) So the Try-Catch is there to stop the script and handle the error only if you don't want the standard PS error handling\display to log\deal with it....?

    3) The behavior of all errors in PowerShell with erroractionpreference=0 is to continue the next line silently? No matter terminating\non-terminating.

    Are those statements basically correct?

    Friday, June 29, 2018 4:39 PM
  • Its a start.

    help about_try_catch

    Search for articles on PowerShell error handling.  It is quite complex.


    \_(ツ)_/

    Friday, June 29, 2018 4:48 PM