1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 /*=============================================================================
9 ** Purpose: Exception class for invalid arguments to a method.
12 =============================================================================*/
14 using System
.Globalization
;
15 using System
.Runtime
.Serialization
;
19 // The ArgumentException is thrown when an argument does not meet
20 // the contract of the method. Ideally it should give a meaningful error
21 // message describing what was wrong and which parameter is incorrect.
23 [System
.Runtime
.CompilerServices
.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
24 public class ArgumentException
: SystemException
26 private readonly string? _paramName
;
28 // Creates a new ArgumentException with its message
29 // string set to the empty string.
30 public ArgumentException()
31 : base(SR
.Arg_ArgumentException
)
33 HResult
= HResults
.COR_E_ARGUMENT
;
36 // Creates a new ArgumentException with its message
37 // string set to message.
39 public ArgumentException(string? message
)
42 HResult
= HResults
.COR_E_ARGUMENT
;
45 public ArgumentException(string? message
, Exception
? innerException
)
46 : base(message
, innerException
)
48 HResult
= HResults
.COR_E_ARGUMENT
;
51 public ArgumentException(string? message
, string? paramName
, Exception
? innerException
)
52 : base(message
, innerException
)
54 _paramName
= paramName
;
55 HResult
= HResults
.COR_E_ARGUMENT
;
58 public ArgumentException(string? message
, string? paramName
)
61 _paramName
= paramName
;
62 HResult
= HResults
.COR_E_ARGUMENT
;
65 protected ArgumentException(SerializationInfo info
, StreamingContext context
)
68 _paramName
= info
.GetString("ParamName");
71 public override void GetObjectData(SerializationInfo info
, StreamingContext context
)
73 base.GetObjectData(info
, context
);
74 info
.AddValue("ParamName", _paramName
, typeof(string));
77 public override string Message
83 string s
= base.Message
;
84 if (!string.IsNullOrEmpty(_paramName
))
86 s
+= " " + SR
.Format(SR
.Arg_ParamName_Name
, _paramName
);
93 private void SetMessageField()
95 if (_message
== null && HResult
== System
.HResults
.COR_E_ARGUMENT
)
97 _message
= SR
.Arg_ArgumentException
;
101 public virtual string? ParamName
=> _paramName
;