MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Runtime.Serialization;5 6namespace System.Management.Automation7{8 /// <summary>9 /// This is a wrapper for exception class10 /// <see cref="System.ArgumentNullException"/>11 /// which provides additional information via12 /// <see cref="System.Management.Automation.IContainsErrorRecord"/>.13 /// </summary>14 /// <remarks>15 /// Instances of this exception class are usually generated by the16 /// PowerShell Engine. It is unusual for code outside the PowerShell Engine17 /// to create an instance of this class.18 /// </remarks>19 public class PSArgumentNullException20 : ArgumentNullException, IContainsErrorRecord21 {22 #region ctor23 /// <summary>24 /// Initializes a new instance of the PSArgumentNullException class.25 /// </summary>26 /// <returns>Constructed object.</returns>27 public PSArgumentNullException()28 : base()29 {30 }31 32 /// <summary>33 /// Initializes a new instance of the PSArgumentNullException class.34 /// </summary>35 /// <param name="paramName"></param>36 /// <returns>Constructed object.</returns>37 /// <remarks>38 /// Per MSDN, the parameter is paramName and not message.39 /// I confirm this experimentally as well.40 /// </remarks>41 public PSArgumentNullException(string paramName)42 : base(paramName)43 {44 }45 46 /// <summary>47 /// Initializes a new instance of the PSArgumentNullException class.48 /// </summary>49 /// <param name="message"></param>50 /// <param name="innerException"></param>51 /// <returns>Constructed object.</returns>52 public PSArgumentNullException(string message, Exception innerException)53 : base(message, innerException)54 {55 _message = message;56 }57 58 /// <summary>59 /// Initializes a new instance of the PSArgumentNullException class.60 /// </summary>61 /// <param name="paramName"></param>62 /// <param name="message"></param>63 /// <returns>Constructed object.</returns>64 /// <remarks>65 /// ArgumentNullException has this ctor form and we imitate it here.66 /// </remarks>67 public PSArgumentNullException(string paramName, string message)68 : base(paramName, message)69 {70 _message = message;71 }72 73 #region Serialization74 /// <summary>75 /// Initializes a new instance of the PSArgumentNullException class76 /// using data serialized via77 /// <see cref="System.Runtime.Serialization.ISerializable"/>78 /// </summary>79 /// <param name="info">Serialization information.</param>80 /// <param name="context">Streaming context.</param>81 /// <returns>Constructed object.</returns>82 [Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")]83 protected PSArgumentNullException(SerializationInfo info,84 StreamingContext context)85 {86 throw new NotSupportedException();87 }88 #endregion Serialization89 #endregion ctor90 91 /// <summary>92 /// Additional information about the error.93 /// </summary>94 /// <value></value>95 /// <remarks>96 /// Note that ErrorRecord.Exception is97 /// <see cref="System.Management.Automation.ParentContainsErrorRecordException"/>.98 /// </remarks>99 public ErrorRecord ErrorRecord100 {101 get102 {103 _errorRecord ??= new ErrorRecord(104 new ParentContainsErrorRecordException(this),105 _errorId,106 ErrorCategory.InvalidArgument,107 null);108 109 return _errorRecord;110 }111 }112 113 private ErrorRecord _errorRecord;114 private readonly string _errorId = "ArgumentNull";115 116 /// <summary>117 /// See <see cref="System.Exception.Message"/>118 /// </summary>119 /// <remarks>120 /// Exception.Message is get-only, but you can effectively121 /// set it in a subclass by overriding this virtual property.122 /// </remarks>123 /// <value></value>124 public override string Message125 {126 get { return string.IsNullOrEmpty(_message) ? base.Message : _message; }127 }128 129 private readonly string _message;130 }131}132 