Skip to content

Instantly share code, notes, and snippets.

@PlagueHO
Created January 29, 2016 19:50
Show Gist options
  • Save PlagueHO/f7950fd5c5f249833e97 to your computer and use it in GitHub Desktop.
Save PlagueHO/f7950fd5c5f249833e97 to your computer and use it in GitHub Desktop.
PowerShell code for throwing a custom terminating exception
$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