ArgumentException.cs 3.2 KB

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