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.ArgumentException"/>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 PSArgumentException20 : ArgumentException, IContainsErrorRecord21 {22 #region ctor23 /// <summary>24 /// Initializes a new instance of the PSArgumentException class.25 /// </summary>26 /// <returns>Constructed object.</returns>27 public PSArgumentException()28 : base()29 {30 }31 32 /// <summary>33 /// Initializes a new instance of the PSArgumentException class.34 /// </summary>35 /// <param name="message"></param>36 /// <returns>Constructed object.</returns>37 /// <remarks>38 /// Per MSDN, the parameter is message.39 /// I confirm this experimentally as well.40 /// </remarks>41 public PSArgumentException(string message)42 : base(message)43 {44 }45 46 /// <summary>47 /// Initializes a new instance of the PSArgumentException class.48 /// </summary>49 /// <param name="paramName"></param>50 /// <param name="message"></param>51 /// <returns>Constructed object.</returns>52 /// <remarks>53 /// Note the unusual order of the construction parameters.54 /// ArgumentException has this ctor form and we imitate it here.55 /// </remarks>56 public PSArgumentException(string message, string paramName)57 : base(message, paramName)58 {59 _message = message;60 }61 62 #region Serialization63 /// <summary>64 /// Initializes a new instance of the PSArgumentException class65 /// using data serialized via66 /// <see cref="System.Runtime.Serialization.ISerializable"/>67 /// </summary>68 /// <param name="info">Serialization information.</param>69 /// <param name="context">Streaming context.</param>70 /// <returns>Constructed object.</returns>71 [Obsolete("Legacy serialization support is deprecated since .NET 8", DiagnosticId = "SYSLIB0051")]72 protected PSArgumentException(SerializationInfo info,73 StreamingContext context)74 {75 throw new NotSupportedException();76 }77 78 #endregion Serialization79 80 /// <summary>81 /// Initializes a new instance of the PSArgumentException class.82 /// </summary>83 /// <param name="message"></param>84 /// <param name="innerException"></param>85 /// <returns>Constructed object.</returns>86 public PSArgumentException(string message,87 Exception innerException)88 : base(message, innerException)89 {90 _message = message;91 }92 #endregion ctor93 94 /// <summary>95 /// Additional information about the error.96 /// </summary>97 /// <value></value>98 /// <remarks>99 /// Note that ErrorRecord.Exception is100 /// <see cref="System.Management.Automation.ParentContainsErrorRecordException"/>.101 /// </remarks>102 public ErrorRecord ErrorRecord103 {104 get105 {106 _errorRecord ??= new ErrorRecord(107 new ParentContainsErrorRecordException(this),108 _errorId,109 ErrorCategory.InvalidArgument,110 null);111 112 return _errorRecord;113 }114 }115 116 private ErrorRecord _errorRecord;117 private readonly string _errorId = "Argument";118 119 /// <summary>120 /// See <see cref="System.Exception.Message"/>121 /// </summary>122 /// <remarks>123 /// Exception.Message is get-only, but you can effectively124 /// set it in a subclass by overriding this virtual property.125 /// </remarks>126 /// <value></value>127 public override string Message128 {129 get { return string.IsNullOrEmpty(_message) ? base.Message : _message; }130 }131 132 private readonly string _message;133 }134}135 