ArgumentOutOfRangeException.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 method arguments outside of the legal range.
  9. **
  10. **
  11. =============================================================================*/
  12. using System.Globalization;
  13. using System.Runtime.Serialization;
  14. namespace System
  15. {
  16. // The ArgumentOutOfRangeException is thrown when an argument
  17. // is outside the legal range for that argument.
  18. [Serializable]
  19. [System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
  20. public class ArgumentOutOfRangeException : ArgumentException
  21. {
  22. private object _actualValue;
  23. // Creates a new ArgumentOutOfRangeException with its message
  24. // string set to a default message explaining an argument was out of range.
  25. public ArgumentOutOfRangeException()
  26. : base(SR.Arg_ArgumentOutOfRangeException)
  27. {
  28. HResult = HResults.COR_E_ARGUMENTOUTOFRANGE;
  29. }
  30. public ArgumentOutOfRangeException(string paramName)
  31. : base(SR.Arg_ArgumentOutOfRangeException, paramName)
  32. {
  33. HResult = HResults.COR_E_ARGUMENTOUTOFRANGE;
  34. }
  35. public ArgumentOutOfRangeException(string paramName, string message)
  36. : base(message, paramName)
  37. {
  38. HResult = HResults.COR_E_ARGUMENTOUTOFRANGE;
  39. }
  40. public ArgumentOutOfRangeException(string message, Exception innerException)
  41. : base(message, innerException)
  42. {
  43. HResult = HResults.COR_E_ARGUMENTOUTOFRANGE;
  44. }
  45. // We will not use this in the classlibs, but we'll provide it for
  46. // anyone that's really interested so they don't have to stick a bunch
  47. // of printf's in their code.
  48. public ArgumentOutOfRangeException(string paramName, object actualValue, string message)
  49. : base(message, paramName)
  50. {
  51. _actualValue = actualValue;
  52. HResult = HResults.COR_E_ARGUMENTOUTOFRANGE;
  53. }
  54. protected ArgumentOutOfRangeException(SerializationInfo info, StreamingContext context)
  55. : base(info, context)
  56. {
  57. _actualValue = info.GetValue("ActualValue", typeof(object));
  58. }
  59. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  60. {
  61. base.GetObjectData(info, context);
  62. info.AddValue("ActualValue", _actualValue, typeof(object));
  63. }
  64. public override string Message
  65. {
  66. get
  67. {
  68. string s = base.Message;
  69. if (_actualValue != null)
  70. {
  71. string valueMessage = SR.Format(SR.ArgumentOutOfRange_ActualValue, _actualValue.ToString());
  72. if (s == null)
  73. return valueMessage;
  74. return s + Environment.NewLine + valueMessage;
  75. }
  76. return s;
  77. }
  78. }
  79. // Gets the value of the argument that caused the exception.
  80. // Note - we don't set this anywhere in the class libraries in
  81. // version 1, but it might come in handy for other developers who
  82. // want to avoid sticking printf's in their code.
  83. public virtual object ActualValue
  84. {
  85. get { return _actualValue; }
  86. }
  87. }
  88. }