// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System.Runtime.Serialization; namespace System.Management.Automation { /// /// This is a wrapper for exception class /// /// which provides additional information via /// . /// /// /// Instances of this exception class are usually generated by the /// PowerShell Engine. It is unusual for code outside the PowerShell Engine /// to create an instance of this class. /// public class PSArgumentNullException : ArgumentNullException, IContainsErrorRecord { #region ctor /// /// Initializes a new instance of the PSArgumentNullException class. /// /// Constructed object. public PSArgumentNullException() : base() { } /// /// Initializes a new instance of the PSArgumentNullException class. /// /// /// Constructed object. /// /// Per MSDN, the parameter is paramName and not message. /// I confirm this experimentally as well. /// public PSArgumentNullException(string paramName) : base(paramName) { } /// /// Initializes a new instance of the PSArgumentNullException class. /// /// /// /// Constructed object. public PSArgumentNullException(string message, Exception innerException) : base(message, innerException) { _message = message; } /// /// Initializes a new instance of the PSArgumentNullException class. /// /// /// /// Constructed object. /// /// ArgumentNullException has this ctor form and we imitate it here. /// public PSArgumentNullException(string paramName, string message) : base(paramName, message) { _message = message; } #region Serialization /// /// Initializes a new instance of the PSArgumentNullException class /// using data serialized via /// /// /// Serialization information. /// Streaming context. /// Constructed object. [Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")] protected PSArgumentNullException(SerializationInfo info, StreamingContext context) { throw new NotSupportedException(); } #endregion Serialization #endregion ctor /// /// Additional information about the error. /// /// /// /// Note that ErrorRecord.Exception is /// . /// public ErrorRecord ErrorRecord { get { _errorRecord ??= new ErrorRecord( new ParentContainsErrorRecordException(this), _errorId, ErrorCategory.InvalidArgument, null); return _errorRecord; } } private ErrorRecord _errorRecord; private readonly string _errorId = "ArgumentNull"; /// /// See /// /// /// Exception.Message is get-only, but you can effectively /// set it in a subclass by overriding this virtual property. /// /// public override string Message { get { return string.IsNullOrEmpty(_message) ? base.Message : _message; } } private readonly string _message; } }