ValueOutOfRangeException.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Runtime.Serialization;
  3. namespace System.Net.ApiService.Compression.Exceptions {
  4. /// <summary>
  5. /// Indicates that a value was outside of the expected range when decoding an input stream
  6. /// </summary>
  7. [Serializable]
  8. public class ValueOutOfRangeException : StreamDecodingException
  9. {
  10. /// <summary>
  11. /// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable
  12. /// </summary>
  13. /// <param name="nameOfValue">Name of the variable, use: nameof()</param>
  14. public ValueOutOfRangeException(string nameOfValue)
  15. : base($"{nameOfValue} out of range") { }
  16. /// <summary>
  17. /// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable,
  18. /// it's current value and expected range.
  19. /// </summary>
  20. /// <param name="nameOfValue">Name of the variable, use: nameof()</param>
  21. /// <param name="value">The invalid value</param>
  22. /// <param name="maxValue">Expected maximum value</param>
  23. /// <param name="minValue">Expected minimum value</param>
  24. public ValueOutOfRangeException(string nameOfValue, long value, long maxValue, long minValue = 0)
  25. : this(nameOfValue, value.ToString(), maxValue.ToString(), minValue.ToString()) { }
  26. /// <summary>
  27. /// Initializes a new instance of the ValueOutOfRangeException class naming the the causing variable,
  28. /// it's current value and expected range.
  29. /// </summary>
  30. /// <param name="nameOfValue">Name of the variable, use: nameof()</param>
  31. /// <param name="value">The invalid value</param>
  32. /// <param name="maxValue">Expected maximum value</param>
  33. /// <param name="minValue">Expected minimum value</param>
  34. public ValueOutOfRangeException(string nameOfValue, string value, string maxValue, string minValue = "0") :
  35. base($"{nameOfValue} out of range: {value}, should be {minValue}..{maxValue}")
  36. { }
  37. private ValueOutOfRangeException()
  38. {
  39. }
  40. private ValueOutOfRangeException(string message, Exception innerException) : base(message, innerException)
  41. {
  42. }
  43. /// <summary>
  44. /// Initializes a new instance of the ValueOutOfRangeException class with serialized data.
  45. /// </summary>
  46. /// <param name="info">
  47. /// The System.Runtime.Serialization.SerializationInfo that holds the serialized
  48. /// object data about the exception being thrown.
  49. /// </param>
  50. /// <param name="context">
  51. /// The System.Runtime.Serialization.StreamingContext that contains contextual information
  52. /// about the source or destination.
  53. /// </param>
  54. protected ValueOutOfRangeException(SerializationInfo info, StreamingContext context)
  55. : base(info, context)
  56. {
  57. }
  58. }
  59. }