The Labs \ Source Viewer \ SSCLI \ System \ ArgumentOutOfRangeException

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. /*=============================================================================
  16. **
  17. ** Class: ArgumentOutOfRangeException
  18. **
  19. **
  20. ** Purpose: Exception class for method arguments outside of the legal range.
  21. **
  22. **
  23. =============================================================================*/
  24. namespace System
  25. {
  26.    
  27.     using System;
  28.     using System.Runtime.Remoting;
  29.     using System.Runtime.Serialization;
  30.     using System.Globalization;
  31.     using System.Security.Permissions;
  32.    
  33.     // The ArgumentOutOfRangeException is thrown when an argument
  34.     // is outside the legal range for that argument.
  35.     [System.Runtime.InteropServices.ComVisible(true)]
  36.     [Serializable()]
  37.     public class ArgumentOutOfRangeException : ArgumentException, ISerializable
  38.     {
  39.        
  40.         private static string _rangeMessage;
  41.         private object m_actualValue;
  42.        
  43.         private static string RangeMessage {
  44.             get {
  45.                 if (_rangeMessage == null)
  46.                     _rangeMessage = Environment.GetResourceString("Arg_ArgumentOutOfRangeException");
  47.                 return _rangeMessage;
  48.             }
  49.         }
  50.        
  51.         // Creates a new ArgumentOutOfRangeException with its message
  52.         // string set to a default message explaining an argument was out of range.
  53.         public ArgumentOutOfRangeException() : base(RangeMessage)
  54.         {
  55.             SetErrorCode(__HResults.COR_E_ARGUMENTOUTOFRANGE);
  56.         }
  57.        
  58.         public ArgumentOutOfRangeException(string paramName) : base(RangeMessage, paramName)
  59.         {
  60.             SetErrorCode(__HResults.COR_E_ARGUMENTOUTOFRANGE);
  61.         }
  62.        
  63.         public ArgumentOutOfRangeException(string paramName, string message) : base(message, paramName)
  64.         {
  65.             SetErrorCode(__HResults.COR_E_ARGUMENTOUTOFRANGE);
  66.         }
  67.        
  68.         public ArgumentOutOfRangeException(string message, Exception innerException) : base(message, innerException)
  69.         {
  70.             SetErrorCode(__HResults.COR_E_ARGUMENTOUTOFRANGE);
  71.         }
  72.        
  73.         // We will not use this in the classlibs, but we'll provide it for
  74.         // anyone that's really interested so they don't have to stick a bunch
  75.         // of printf's in their code.
  76.         public ArgumentOutOfRangeException(string paramName, object actualValue, string message) : base(message, paramName)
  77.         {
  78.             m_actualValue = actualValue;
  79.             SetErrorCode(__HResults.COR_E_ARGUMENTOUTOFRANGE);
  80.         }
  81.        
  82.         public override string Message {
  83.             get {
  84.                 string s = base.Message;
  85.                 if (m_actualValue != null) {
  86.                     string valueMessage = String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("ArgumentOutOfRange_ActualValue"), m_actualValue.ToString());
  87.                     if (s == null)
  88.                         return valueMessage;
  89.                     return s + Environment.NewLine + valueMessage;
  90.                 }
  91.                 return s;
  92.             }
  93.         }
  94.        
  95.         // Gets the value of the argument that caused the exception.
  96.         // Note - we don't set this anywhere in the class libraries in
  97.         // version 1, but it might come in handy for other developers who
  98.         // want to avoid sticking printf's in their code.
  99.         public virtual object ActualValue {
  100.             get { return m_actualValue; }
  101.         }
  102.        
  103.         [SecurityPermissionAttribute(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
  104.         public override void GetObjectData(SerializationInfo info, StreamingContext context)
  105.         {
  106.             if (info == null) {
  107.                 throw new ArgumentNullException("info");
  108.             }
  109.             base.GetObjectData(info, context);
  110.             info.AddValue("ActualValue", m_actualValue, typeof(object));
  111.         }
  112.        
  113.         protected ArgumentOutOfRangeException(SerializationInfo info, StreamingContext context) : base(info, context)
  114.         {
  115.             m_actualValue = info.GetValue("ActualValue", typeof(object));
  116.         }
  117.     }
  118. }

Developer Fusion