Created
January 29, 2016 19:50
-
-
Save PlagueHO/f7950fd5c5f249833e97 to your computer and use it in GitHub Desktop.
PowerShell code for throwing a custom terminating exception
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ErrorParam = @{ | |
ErrorId = 'NICNotFound' | |
ErrorMessage = ($LocalizedData.NICNotFound -f $InterfaceAlias) | |
ErrorCategory = 'ObjectNotFound' | |
ErrorAction = 'Stop' | |
} | |
New-TerminatingError @ErrorParam | |
function New-TerminatingError | |
{ | |
[CmdletBinding()] | |
param | |
( | |
[Parameter(Mandatory)] | |
[String] $ErrorId, | |
[Parameter(Mandatory)] | |
[String] $ErrorMessage, | |
[Parameter(Mandatory)] | |
[System.Management.Automation.ErrorCategory] $ErrorCategory | |
) | |
$exception = New-Object System.InvalidOperationException $errorMessage | |
$errorRecord = New-Object System.Management.Automation.ErrorRecord $exception, $errorId, $errorCategory, $null | |
throw $errorRecord | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment