// 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 /// Monad Engine. It is unusual for code outside the Monad Engine /// to create an instance of this class. /// [Serializable] public class PSArgumentOutOfRangeException : ArgumentOutOfRangeException, IContainsErrorRecord { #region ctor /// /// Constructor for class PSArgumentOutOfRangeException. /// /// Constructed object. public PSArgumentOutOfRangeException() : base() { } /// /// Initializes a new instance of the PSArgumentOutOfRangeException class. /// /// /// Constructed object. /// /// Per MSDN, the parameter is paramName and not message. /// I confirm this experimentally as well. /// public PSArgumentOutOfRangeException(string paramName) : base(paramName) { } /// /// Initializes a new instance of the PSArgumentOutOfRangeException class. /// /// /// /// /// Constructed object. /// /// ArgumentOutOfRangeException has this ctor form and we imitate it here. /// public PSArgumentOutOfRangeException(string paramName, object actualValue, string message) : base(paramName, actualValue, message) { } #region Serialization /// /// Initializes a new instance of the PSArgumentOutOfRangeException class /// using data serialized via /// /// /// Serialization information. /// Streaming context. /// Constructed object. protected PSArgumentOutOfRangeException(SerializationInfo info, StreamingContext context) : base(info, context) { _errorId = info.GetString("ErrorId"); } /// /// Serializer for /// /// Serialization information. /// Streaming context. public override void GetObjectData(SerializationInfo info, StreamingContext context) { if (info == null) { throw new PSArgumentNullException(nameof(info)); } base.GetObjectData(info, context); info.AddValue("ErrorId", _errorId); } #endregion Serialization /// /// Initializes a new instance of the PSArgumentOutOfRangeException class. /// /// /// /// Constructed object. public PSArgumentOutOfRangeException(string message, Exception innerException) : base(message, innerException) { } #endregion ctor /// /// Additional information about the error. /// /// /// /// Note that ErrorRecord.Exception is /// . /// public ErrorRecord ErrorRecord { get { if (_errorRecord == null) { _errorRecord = new ErrorRecord( new ParentContainsErrorRecordException(this), _errorId, ErrorCategory.InvalidArgument, null); } return _errorRecord; } } private ErrorRecord _errorRecord; private readonly string _errorId = "ArgumentOutOfRange"; } }