[powershell] When should I use Write-Error vs. Throw? Terminating vs. non-terminating errors

Looking at a Get-WebFile script over on PoshCode, http://poshcode.org/3226, I noticed this strange-to-me contraption:

$URL_Format_Error = [string]"..."
Write-Error $URL_Format_Error
return

What is the reason for this as opposed to the following?

$URL_Format_Error = [string]"..."
Throw $URL_Format_Error

Or even better:

$URL_Format_Error = New-Object System.FormatException "..."
Throw $URL_Format_Error

As I understand, you should use Write-Error for non-terminating errors, and Throw for terminating errors, so it seems to me that you should not use Write-Error followed by Return. Is there a difference?

This question is related to powershell error-handling powershell-2.0

The answer is


Examples related to powershell

Why powershell does not run Angular commands? How do I install the Nuget provider for PowerShell on a unconnected machine so I can install a nuget package from the PS command line? How to print environment variables to the console in PowerShell? Check if a string is not NULL or EMPTY The term 'ng' is not recognized as the name of a cmdlet VSCode Change Default Terminal 'Connect-MsolService' is not recognized as the name of a cmdlet Powershell Invoke-WebRequest Fails with SSL/TLS Secure Channel Install-Module : The term 'Install-Module' is not recognized as the name of a cmdlet Change directory in PowerShell

Examples related to error-handling

must declare a named package eclipse because this compilation unit is associated to the named module Error:Failed to open zip file. Gradle's dependency cache may be corrupt What does 'index 0 is out of bounds for axis 0 with size 0' mean? What's the source of Error: getaddrinfo EAI_AGAIN? Error handling with try and catch in Laravel What does "Fatal error: Unexpectedly found nil while unwrapping an Optional value" mean? Raise error in a Bash script Javascript Uncaught TypeError: Cannot read property '0' of undefined Multiple values in single-value context IndexError: too many indices for array

Examples related to powershell-2.0

Extract the filename from a path Out-File -append in Powershell does not produce a new line and breaks string into characters Using PowerShell to remove lines from a text file if it contains a string PowerShell The term is not recognized as cmdlet function script file or operable program Can't install nuget package because of "Failed to initialize the PowerShell host" Powershell script to see currently logged in users (domain and machine) + status (active, idle, away) How to export data to CSV in PowerShell? powershell is missing the terminator: " PowerShell script to check the status of a URL How to upgrade PowerShell version from 2.0 to 3.0