[Serializable] |
ArgumentOutOfRangeException is used extensively by:
ArgumentOutOfRangeException behaves identically to ArgumentException. It is provided so that application code can differentiate between exceptions caused by invalid arguments that are not null, and exceptions caused by null arguments. For errors caused by null arguments, see ArgumentNullException.
ArgumentOutOfRangeException uses the HRESULT COR_E_ARGUMENTOUTOFRANGE, which has the value 0x80131502.
For a list of initial property values for an instance of ArgumentOutOfRangeException, see the ArgumentOutOfRangeException.#ctor constructors.
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the ArgumentOutOfRangeException class. |
ctor #2 | Overloaded:.ctor(string paramName) Initializes a new instance of the ArgumentOutOfRangeException class with the name of the parameter that causes this exception. |
ctor #4 | Overloaded:.ctor(string paramName, string message) Initializes a new instance of the ArgumentOutOfRangeException class with a specified error message and the name of the parameter that causes this exception. |
ctor #5 | Overloaded:.ctor(string paramName, object actualValue, string message) Initializes a new instance of the ArgumentOutOfRangeException class with a specified error message, the parameter name, and the value of the argument. |
ActualValue | Read-only Gets the argument value that causes this exception. |
HelpLink (inherited from System.Exception) |
Read-write See base class member description: System.Exception.HelpLink Gets or sets a link to the help file associated with this exception. |
InnerException (inherited from System.Exception) |
Read-only See base class member description: System.Exception.InnerException Gets the Exception instance that caused the current exception. |
Message | Read-only Overridden: Gets the error message and the string representation of the invalid argument value, or only the error message if the argument value is null. |
ParamName (inherited from System.ArgumentException) |
Read-only See base class member description: System.ArgumentException.ParamName Gets the name of the parameter that causes this exception. |
Source (inherited from System.Exception) |
Read-write See base class member description: System.Exception.Source Gets or sets the name of the application or the object that causes the error. |
StackTrace (inherited from System.Exception) |
Read-only See base class member description: System.Exception.StackTrace Gets a string representation of the frames on the call stack at the time the current exception was thrown. |
TargetSite (inherited from System.Exception) |
Read-only See base class member description: System.Exception.TargetSite Gets the method that throws the current exception. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetBaseException (inherited from System.Exception) |
See base class member description: System.Exception.GetBaseException When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetObjectData | Overridden: Sets the SerializationInfo object with the invalid argument value and additional exception information. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
ToString (inherited from System.Exception) |
See base class member description: System.Exception.ToString Creates and returns a string representation of the current exception. |
ctor #3 | Overloaded:.ctor(SerializationInfo info, StreamingContext context) Initializes a new instance of the ArgumentOutOfRangeException class with serialized data. |
HResult (inherited from System.Exception) |
Read-write See base class member description: System.Exception.HResult Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public ArgumentOutOfRangeException(); |
The following table shows the initial property values for an instance of ArgumentOutOfRangeException.
Property | Value |
---|---|
Exception.InnerException | A null reference ( not set or empty in Visual Basic). (Visual Basic not implemented in the shared source CLI) |
ArgumentOutOfRangeException.Message | The localized error message string. |
public ArgumentOutOfRangeException( |
paramName
This constructor initializes the ArgumentException.ParamName property of the new instance using paramName. The content of paramName is intended to be understood by humans.
The following table shows the initial property values for an instance of ArgumentOutOfRangeException.
Property | Value |
---|---|
Exception.InnerException | A null reference ( not set or empty in Visual Basic). (Visual Basic not implemented in the shared source CLI) |
ArgumentOutOfRangeException.Message | The empty string (""). |
ParamName | The parameter name string. |
protected ArgumentOutOfRangeException( |
info
context
paramName
message
This constructor initializes the ArgumentException.ParamName property of the new instance using paramName. The content of paramName is intended to be understood by humans.
The following table shows the initial property values for an instance of ArgumentOutOfRangeException.
Property | Value |
---|---|
Exception.InnerException | null . |
ArgumentOutOfRangeException.Message | The error message string. |
ArgumentException.ParamName | The parameter name string. |
public ArgumentOutOfRangeException( |
paramName
actualValue
message
This constructor with the additional argument actualValue is not used within the .NET Framework class library. The ArgumentOutOfRangeException.ActualValue property is provided so that applications can make use of the available argument value.
The actualValue parameter contains an invalid value that is passed to the method and causes this exception to be thrown. This value is stored in the ArgumentOutOfRangeException.ActualValue property and its string representation is appended to the message string held in the ArgumentOutOfRangeException.Message property.
The following table shows the initial property values for an instance of ArgumentOutOfRangeException.
Property | Value |
---|---|
ArgumentOutOfRangeException.ActualValue | The argument value. |
Exception.InnerException | The null reference ( not set or empty in Visual Basic). (Visual Basic not implemented in the shared source CLI) |
ArgumentOutOfRangeException.Message | The error message string. |
ArgumentException.ParamName | The parameter name string. |
public virtual object ActualValue {get;}
|
The ActualValue property returns the same value as was passed into the constructor, or null if no value was supplied.
The ArgumentOutOfRangeException.ActualValue property is assigned a value at the time of object construction. If the ArgumentOutOfRangeException.ActualValue property value is not null, a string representation of the value is then appended to the message string held by the ArgumentOutOfRangeException.Message property.
The ArgumentOutOfRangeException.ActualValue property is not used within the .NET Framework class library. It carries a null value in all the ArgumentOutOfRangeException objects thrown by the .NET Framework class library. The ArgumentOutOfRangeException.ActualValue property is provided so that applications can make use of the available argument value.
This property is read-only and should only be overridden in order to customize the content or format of the value.
public virtual string HelpLink {get; set;}
|
"file:///C:/Applications/Bazzal/help.html#ErrorNum42"
protected int HResult {get; set;}
|
public Exception InnerException {get;}
|
Use the InnerException property to obtain the set of exceptions that led to the current exception.
You can create a new exception that catches an earlier exception. The code that handles the second exception can make use of the additional information from the earlier exception to handle the error more appropriately.
Suppose that there is a function that reads a file and formats the data from that file. In this example, as the code tries to read the file, an IOException is thrown. The function catches the IOException and throws a FileNotFoundException. The IOException could be saved in the Exception.InnerException property of the FileNotFoundException, enabling the code that catches the FileNotFoundException to examine what causes the initial error.
The Exception.InnerException property, which holds a reference to the inner exception, is set upon initialization of the exception object.
using System; public class MyAppException:ApplicationException { public MyAppException (String message) : base (message) {} public MyAppException (String message, Exception inner) : base(message,inner) {} } public class ExceptExample { public void ThrowInner () { throw new MyAppException("ExceptExample inner exception"); } public void CatchInner() { try { this.ThrowInner(); } catch (Exception e) { throw new MyAppException("Error caused by trying ThrowInner.",e); } } } public class Test { public static void Main() { ExceptExample testInstance = new ExceptExample(); try { testInstance.CatchInner(); } catch(Exception e) { Console.WriteLine ("In Main catch block. Caught: {0}", e.Message); Console.WriteLine ("Inner Exception is {0}",e.InnerException); } } }
This code has the following output:
In Main
catch block. Caught: Error caused by trying ThrowInner. Inner Exception is
MyAppException: ExceptExample inner exception at ExceptExample.ThrowInner() at
ExceptExample.CatchInner()
public override string Message {get;}
|
The error message should describe the expected values of the parameter that causes this exception. The error message should be localized.
public virtual string ParamName {get;}
|
This property is read-only, and returns the same value as was passed into the constructor. Overriding methods should be used solely to customize the content or format of the parameter name.
public virtual string Source {get; set;}
|
public virtual string StackTrace {get;}
|
StackTrace may not report as many method calls as expected, due to code transformations, such as inlining, that occur during optimization.
By default, the stack trace is captured immediately before an exception object is thrown. Use Environment.StackTrace to get stack trace information when no exception is being thrown.
public MethodBase TargetSite {get;}
|
~ArgumentOutOfRangeException(); |
public virtual Exception GetBaseException(); |
For all exceptions in a chain of exceptions, the GetBaseException method must return the same object (the base exception).
Use the GetBaseException method when you want to find the root cause of an exception but do not need information about exceptions that may have occurred between the current exception and the first exception.
public virtual int GetHashCode(); |
public override void GetObjectData( |
info
context
Exception Type | Condition |
---|---|
ArgumentNullException | The info object is null. |
For more information, see SerializationInfo.
public Type GetType(); |
protected object MemberwiseClone(); |
public override string ToString(); |
The default implementation of Exception.ToString obtains the name of the class that threw the current exception, the message, the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace. If any of these members is a null reference (not set or empty), its value is not included in the returned string.
If there is no error message or if it is an empty string (""), then no error message is returned. The name of the inner exception and the stack trace are returned only if they are not a null reference.
This method overrides Object.ToString.
using System; public class MyClass {} public class ArgExceptionExample { public static void Main() { MyClass my = new MyClass(); string s = "sometext"; try { int i = s.CompareTo(my); } catch (Exception e) { Console.WriteLine("Error: {0}",e.ToString()); } } }
This code has the following output:
Error:
System.ArgumentException: Object must be of type String. at
System.String.CompareTo(Object value) at ArgExceptionExample.Main()