System.ArgumentException Class

public class ArgumentException : SystemException

Base Types

Object
  Exception
    SystemException
      ArgumentException

Assembly

mscorlib

Library

BCL

Summary

Represents the error that occurs when an argument passed to a method is invalid.

Description

ArgumentException is thrown when a method is invoked and at least one of the passed arguments does not meet the method's parameter specification.

[Note: The Base Class Library includes three derived types:

When appropriate, use these types instead of ArgumentException.

]

Example

The following example demonstrates an error that causes a ArgumentException exception to be thrown by the system.

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 (ArgumentException e) {
       Console.WriteLine("Error: {0}",e);
    }
  }
}
   
The output is

Error: System.ArgumentException: Object must be of type String.

at System.String.CompareTo(Object value)

at ArgExceptionExample.Main()

See Also

System Namespace

Members

ArgumentException Constructors

ArgumentException() Constructor
ArgumentException(System.String) Constructor
ArgumentException(System.String, System.Exception) Constructor
ArgumentException(System.String, System.String, System.Exception) Constructor
ArgumentException(System.String, System.String) Constructor

ArgumentException Properties

ArgumentException.ParamName Property


ArgumentException() Constructor

public ArgumentException();

Summary

Constructs and initializes a new instance of the ArgumentException class.

Description

This constructor initializes the System.ArgumentException.Message property of the new instance to a system-supplied message that describes the error, such as "An invalid argument was specified." This message takes into account the current system culture.

The System.ArgumentException.InnerException property is initialized to null .

See Also

System.ArgumentException Class, System Namespace

ArgumentException(System.String) Constructor

public ArgumentException(string message);

Summary

Constructs and initializes a new instance of the ArgumentException class.

Parameters

message
A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.

Description

This constructor initializes the System.ArgumentException.Message property of the new instance using message. If message is null , the System.ArgumentException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments. The System.ArgumentException.InnerException and System.ArgumentException.ParamName properties are initialized to null .

See Also

System.ArgumentException Class, System Namespace

ArgumentException(System.String, System.Exception) Constructor

public ArgumentException(string message, Exception innerException);

Summary

Constructs and initializes a new instance of the ArgumentException class.

Parameters

message
A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
innerException
An instance of Exception that is the cause of the current Exception. If innerException is non-null, then the current Exception was raised in a catch block handling innerException .

Description

This constructor initializes the System.ArgumentException.Message property of the new instance using message, and the System.ArgumentException.InnerException property using innerException. If message is null , the System.ArgumentException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments. The System.ArgumentException.ParamName property is initialized to null .

[Note: For information on inner exceptions, see System.Exception.InnerException.]

See Also

System.ArgumentException Class, System Namespace

ArgumentException(System.String, System.String, System.Exception) Constructor

public ArgumentException(string message, string paramName, Exception innerException);

Summary

Constructs and initializes a new instance of the ArgumentException class.

Parameters

message
A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
paramName
A String that contains the name of the parameter that caused the current exception.
innerException
An instance of Exception that is the cause of the current Exception. If innerException is non-null, then the current Exception was raised in a catch block handling innerException.

Description

This constructor initializes the System.ArgumentException.Message property of the new instance using message, the System.ArgumentException.ParamName property using paramName, and the System.ArgumentException.InnerException property using innerException. If message is null , the System.ArgumentException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.

[Note: For information on inner exceptions, see System.Exception.InnerException.]

See Also

System.ArgumentException Class, System Namespace

ArgumentException(System.String, System.String) Constructor

public ArgumentException(string message, string paramName);

Summary

Constructs and initializes a new instance of the ArgumentException class.

Parameters

message
A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
paramName
A String that contains the name of the parameter that caused the exception.

Description

This constructor initializes the System.ArgumentException.Message property of the new instance using message, and the System.ArgumentException.ParamName property using paramName. If message is null , the System.ArgumentException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments. The System.ArgumentException.InnerException property is initialized to null .

See Also

System.ArgumentException Class, System Namespace

ArgumentException.ParamName Property

public virtual string ParamName { get; }

Summary

Gets the name of the parameter that caused the current Exception.

Property Value

A String that contains the name of the parameter that caused the current Exception, or null if no value was specified to the constructor for the current instance.

Description

This property is read-only.

[Behaviors: The System.ArgumentException.ParamName property returns the same value as was passed into the constructor.]

[Overrides: Override the System.ArgumentException.ParamName property to customize the content or format of the parameter name. ]

[Usage: Use this property to retrieve the name of the invalid parameter. ]

See Also

System.ArgumentException Class, System Namespace