Skip to content

New Exception

dscbot edited this page Feb 13, 2024 · 2 revisions

New-Exception

SYNOPSIS

Creates and returns an exception.

SYNTAX

New-Exception [-Message] <String> [[-ErrorRecord] <ErrorRecord>] 
 [<CommonParameters>]

DESCRIPTION

Creates an exception that will be returned.

EXAMPLES

EXAMPLE 1

$errorRecord = New-Exception -Message 'An error occurred'

Creates and returns an exception with the message 'An error occurred'.

EXAMPLE 2

try
{
    Get-ChildItem -Path $path -ErrorAction 'Stop'
}
catch
{
    $exception = New-Exception -Message 'Could not get files' -ErrorRecord $_
}

Returns an exception with the message 'Could not get files' and includes the exception that caused this terminating error.

PARAMETERS

-ErrorRecord

The error record containing the exception that is causing this terminating error.

Type: ErrorRecord
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Message

The message explaining why this error is being thrown.

Type: String
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

None

System.Exception

NOTES

RELATED LINKS

Clone this wiki locally