Microsoft Learn

System.ArgumentException Class

Represents errors that occur because of an invalid argument provided to a method.

The ArgumentException class is the base class for exceptions that are thrown when a method receives an invalid argument. This exception is typically thrown when a parameter in a method call is not valid for some reason.

Table of Contents

Syntax

public class ArgumentException : SystemException

Constructors

Constructor Description
public ArgumentException()
Initializes a new instance of the ArgumentException class.
public ArgumentException(string message)
Initializes a new instance of the ArgumentException class with a specified error message.
public ArgumentException(string message, Exception innerException)
Initializes a new instance of the ArgumentException class with a specified error message and a reference to the inner exception that is the cause of this exception.
public ArgumentException(string message, string paramName)
Initializes a new instance of the ArgumentException class with a specified error message and the name of the parameter that causes this exception.
public ArgumentException(string message, string paramName, Exception innerException)
Initializes a new instance of the ArgumentException class with a specified error message, the name of the parameter that causes this exception, and a reference to the inner exception that is the cause of this exception.

Properties

Property Type Description
Message string Gets an error message that describes the exception that occurred. (Inherited from Exception)
ParamName string Gets the name of the argument that causes this exception.
InnerException Exception Gets a reference to the inner exception that is the cause of this exception. (Inherited from Exception)
StackTrace string Gets a string that represents the immediate frames of the call stack. (Inherited from Exception)
HelpLink string Gets or sets a link to the help file associated with this exception. (Inherited from Exception)
HResult int Gets or sets HRESULT, which is a coded numerical value that is assigned to a specific exception. (Inherited from Exception)

Methods

Method Description
public override string ToString()
Overrides Object.ToString(). Returns a string representation of the current exception. (Inherited from Exception)
protected void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
Sets the SerializationInfo object with information about the exception. (Inherited from Exception)

Code Examples

C# Example C#

using System;

public class Example
{
    public static void Main(string[] args)
    {
        try
        {
            ProcessData(-1);
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine($"Error: {ex.Message}");
            Console.WriteLine($"Parameter name: {ex.ParamName}");
        }
    }

    public static void ProcessData(int value)
    {
        if (value < 0)
        {
            throw new ArgumentException("The value cannot be negative.", nameof(value));
        }
        // ... process data ...
        Console.WriteLine($"Processing value: {value}");
    }
}
        
VB.NET Example VB.NET

Imports System

Public Class Example
    Public Shared Sub Main(args As String())
        Try
            ProcessData(-1)
        Catch ex As ArgumentException
            Console.WriteLine($"Error: {ex.Message}")
            Console.WriteLine($"Parameter name: {ex.ParamName}")
        End Try
    End Sub

    Public Shared Sub ProcessData(value As Integer)
        If value < 0 Then
            Throw New ArgumentException("The value cannot be negative.", NameOf(value))
        End If
        ' ... process data ...
        Console.WriteLine($"Processing value: {value}")
    End Sub
End Class
        

Requirements

Assembly mscorlib.dll
Namespace System
Platform Windows, Linux, macOS
.NET Framework versions Available in .NET Framework 1.1 and later versions.
.NET versions Available in .NET Core 1.0 and later versions, .NET 5 and later versions.