ArgumentException.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.
  4. /*=============================================================================
  5. **
  6. **
  7. **
  8. ** Purpose: Exception class for invalid arguments to a method.
  9. **
  10. **
  11. =============================================================================*/
  12. using System.Runtime.Serialization;
  13. namespace System
  14. {
  15. // The ArgumentException is thrown when an argument does not meet
  16. // the contract of the method. Ideally it should give a meaningful error
  17. // message describing what was wrong and which parameter is incorrect.
  18. [Serializable]
  19. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  20. public class ArgumentException : SystemException
  21. {
  22. private readonly string? _paramName;
  23. // Creates a new ArgumentException with its message
  24. // string set to the empty string.
  25. public ArgumentException()
  26. : base(SR.Arg_ArgumentException)
  27. {
  28. HResult = HResults.COR_E_ARGUMENT;
  29. }
  30. // Creates a new ArgumentException with its message
  31. // string set to message.
  32. //
  33. public ArgumentException(string? message)
  34. : base(message)
  35. {
  36. HResult = HResults.COR_E_ARGUMENT;
  37. }
  38. public ArgumentException(string? message, Exception? innerException)
  39. : base(message, innerException)
  40. {
  41. HResult = HResults.COR_E_ARGUMENT;
  42. }
  43. public ArgumentException(string? message, string? paramName, Exception? innerException)
  44. : base(message, innerException)
  45. {
  46. _paramName = paramName;
  47. HResult = HResults.COR_E_ARGUMENT;
  48. }
  49. public ArgumentException(string? message, string? paramName)
  50. : base(message)
  51. {
  52. _paramName = paramName;
  53. HResult = HResults.COR_E_ARGUMENT;
  54. }
  55. protected ArgumentException(SerializationInfo info, StreamingContext context)
  56. : base(info, context)
  57. {
  58. _paramName = info.GetString("ParamName");
  59. }
  60. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  61. {
  62. base.GetObjectData(info, context);
  63. info.AddValue("ParamName", _paramName, typeof(string));
  64. }
  65. public override string Message
  66. {
  67. get
  68. {
  69. SetMessageField();
  70. string s = base.Message;
  71. if (!string.IsNullOrEmpty(_paramName))
  72. {
  73. s += " " + SR.Format(SR.Arg_ParamName_Name, _paramName);
  74. }
  75. return s;
  76. }
  77. }
  78. private void SetMessageField()
  79. {
  80. if (_message == null && HResult == System.HResults.COR_E_ARGUMENT)
  81. {
  82. _message = SR.Arg_ArgumentException;
  83. }
  84. }
  85. public virtual string? ParamName => _paramName;
  86. }
  87. }