ArgumentOutOfRangeException.cs 3.5 KB

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