Azure PowerShell Exceptions

June 13, 2025 ยท View on GitHub

What are Azure PowerShell Exceptions?

Azure PowerShell defines most commonly used exceptions, all of which inherit from the IContainsAzPSErrorData interface. This interface includes telemetry data. Developers working on Azure PowerShell should use these exceptions during development, rather than other more generic exceptions.

Azure PowerShell Exception TypeDefault Error KindMandatory PropertiesWhen to Use it?
AzPSCloudExceptionServiceHttpStatusCodeThis exception should be thrown for getting incorrect http response from Azure service.
AzPSAuthenticationFailedExceptionInternalAuthErrorCodeThis exception should be thrown for authentication failures in Azure PowerShell.
AzPSResourceNotFoundCloudExceptionUserThis exception should be thrown when the resource is not found by Azure service.
AzPsArgumentExceptionUserParamNameThis exception should be thrown for errors in an arithmetic, casting, or conversion operation.
AzPSArgumentNullExceptionUserParamNameThis exception should be thrown when a null reference (Nothing in Visual Basic) is passed to a method that does not accept it as a valid argument.
AzPSArgumentOutOfRangeExceptionUserParamNameThis exception should be thrown when the argument is out of range.
AzPSExceptionN/AThis exception should be thrown when errors occur during application execution.
AzPSInvalidOperationExceptionInternalThis exception should be thrown when a method call is invalid for the object's current state.
AzPSIOExceptionUserThis exception should be thrown when an I/O error occurs.
AzPSKeyNotFoundExceptionInternalMapKeyNameThis exception should be thrown when the key specified for accessing an element in a collection does not match any key in the collection.
AzPSApplicationExceptionInternalThis exception is representative of ApplicationException in Azure PowerShell.
AzPSFileNotFoundExceptionUserFileName (Not full path)This exception should be thrown when accessing a file that does not exist.

There are three types of errors in Azure PowerShell.

  • User Error: The error is caused by user.
  • Service Error: The error is caused by Azure services.
  • Internal Error: other errors.

How to use Azure PowerShell Exception?

  • An code example for AzPSArgumentException

The source code is from NewAzureRmAks.cs.

throw new AzPSArgumentException(
    Resources.AksNodePoolAutoScalingParametersMustAppearTogether,
    nameof(EnableNodeAutoScaling),
    desensitizedMessage: Resources.AksNodePoolAutoScalingParametersMustAppearTogether);
  • An code example for from AzPSCloudException

The source code is KubeCmdletBase.cs.

var newEx = new AzPSCloudException(Resources.K8sVersionNotSupported, Resources.K8sVersionNotSupported, ex)
    {
        Request = ex.Request,
        Response = ex.Response,
        Body = ex.Body,
    };
throw newEx;
  • An code example for AzPSResourceNotFoundCloudException

The source code is from KubeCmdletBase.cs.

var newEx = new AzPSResourceNotFoundCloudException(ex.Message, innerException: ex)
    {
        Request = ex.Request,
        Response = ex.Response,
        Body = ex.Body,
    };
throw newEx;

Why to use Azure PowerShell Exception?

Providing detailed error information is helpful for developers to pinpoint the root cause of errors. However, due to GDPR (General Data Protection Regulation), we do not record client's detailed error messages in telemetry data. To assist developers in more efficiently locating errors, we define unified exception interfaces that includes additional error details without containing any sensitive information.

Azure PowerShell Exception Telemetry Data

  • An exception telemetry example for AzPSArgumentException
{
"exception-data": "ParamName=context.Account;ErrorKind=User;ErrorLineNumber=289;ErrorFileName=AuthenticationFactory",
"exception-type": "Microsoft.Azure.Commands.Common.Exceptions.AzPSArgumentException"
}
  • An exception telemetry example for generic ArgumentException
{
"exception-type": "System.ArgumentException"
}

Refer to Azure PowerShell Telemetry for more telemetry details.