ArgumentException.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. string s = base.Message;
  71. if (!string.IsNullOrEmpty(_paramName))
  72. {
  73. string resourceString = SR.Format(SR.Arg_ParamName_Name, _paramName);
  74. return s + Environment.NewLine + resourceString;
  75. }
  76. else
  77. return s;
  78. }
  79. }
  80. public virtual string ParamName
  81. {
  82. get { return _paramName; }
  83. }
  84. }
  85. }