更新 : 2007 年 11 月
メソッドに渡された引数のいずれかが無効な場合にスローされる例外。
名前空間 :
System
アセンブリ :
mscorlib (mscorlib.dll 内)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class ArgumentException _
Inherits SystemException _
Implements ISerializable
Dim instance As ArgumentException
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class ArgumentException : SystemException,
ISerializable
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class ArgumentException : public SystemException,
ISerializable
/** @attribute SerializableAttribute */
/** @attribute ComVisibleAttribute(true) */
public class ArgumentException extends SystemException implements ISerializable
public class ArgumentException extends SystemException implements ISerializable
ArgumentException は、メソッドの呼び出し時に渡された引数の少なくとも 1 つが、呼び出されたメソッドのパラメータの仕様に一致していないとスローされます。ArgumentException のすべてのインスタンスは、無効な引数と、その引数に指定できる有効な値の範囲を説明する、わかりやすいエラーメッセージを保持する必要があります。
ArgumentException の主要な派生クラスは、ArgumentNullException と ArgumentOutOfRangeException です。これらの派生クラスは、両方とも使用できない場合以外は、ArgumentException の代わりに使用されます。たとえば、次の場合は、それぞれの派生クラスで例外をスローします。
引数を指定せずにメソッドが呼び出された場合、引数が原因ではないエラーが発生している場合は、InvalidOperationException を使用します。
ArgumentException は、値 0x80070057 を保持する HRESULT COR_E_ARGUMENT を使用します。
ArgumentException のインスタンスの初期プロパティ値の一覧については、ArgumentException コンストラクタのトピックを参照してください。
ArgumentException をスローし、キャッチする方法を次の例に示します。
Public Class App
Public Shared Sub Main()
' ArgumentException is not thrown because 10 is an even number.
Console.WriteLine("10 divided by 2 is {0}", DivideByTwo(10))
Try
' ArgumentException is thrown because 7 is not an even number.
Console.WriteLine("7 divided by 2 is {0}", DivideByTwo(7))
Catch Ex As ArgumentException
' Show the user that 7 cannot be divided by 2.
Console.WriteLine("7 is not divided by 2 integrally.")
End Try
End Sub
Private Shared Function DivideByTwo(ByVal num As Integer) As Integer
' If num is an odd number, throw an ArgumentException.
If ((num And 1) _
= 1) Then
Throw New ArgumentException("Number must be even", "num")
End If
Return (num / 2)
End Function
End Class
' This code produces the following output.
'
' 10 divided by 2 is 5
' 7 is not divided by 2 integrally.
using System;
public sealed class App
{
static void Main()
{
// ArgumentException is not thrown because 10 is an even number.
Console.WriteLine("10 divided by 2 is {0}", DivideByTwo(10));
try
{
// ArgumentException is thrown because 7 is not an even number.
Console.WriteLine("7 divided by 2 is {0}", DivideByTwo(7));
}
catch (ArgumentException)
{
// Show the user that 7 cannot be divided by 2.
Console.WriteLine("7 is not divided by 2 integrally.");
}
}
static int DivideByTwo(int num)
{
// If num is an odd number, throw an ArgumentException.
if ((num & 1) == 1)
throw new ArgumentException("Number must be even", "num");
// num is even, return half of its value.
return num / 2;
}
}
// This code produces the following output.
//
// 10 divided by 2 is 5
// 7 is not divided by 2 integrally.
using namespace System;
int DivideByTwo(int num)
{
// If num is an odd number, throw an ArgumentException.
if ((num & 1) == 1)
{
throw gcnew ArgumentException("Number must be even", "num");
}
// num is even, return half of its value.
return num / 2;
}
int main()
{
// ArgumentException is not thrown because 10 is an even number.
Console::WriteLine("10 divided by 2 is {0}", DivideByTwo(10));
try
{
// ArgumentException is thrown because 7 is not an even number.
Console::WriteLine("7 divided by 2 is {0}", DivideByTwo(7));
}
catch (ArgumentException^)
{
// Show the user that 7 cannot be divided by 2.
Console::WriteLine("7 is not divided by 2 integrally.");
}
}
// This code produces the following output.
//
// 10 divided by 2 is 5
// 7 is not divided by 2 integrally.
System..::.Object
System..::.Exception
System..::.SystemException
System..::.ArgumentException
System..::.ArgumentNullException
System..::.ArgumentOutOfRangeException
System.ComponentModel..::.InvalidAsynchronousStateException
System.ComponentModel..::.InvalidEnumArgumentException
System..::.DuplicateWaitObjectException
System.IO.Log..::.ReservationNotFoundException
System.Text..::.DecoderFallbackException
System.Text..::.EncoderFallbackException
この型のすべてのパブリック static (Visual Basic では Shared) メンバは、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360
.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
.NET Framework
サポート対象 : 3.5、3.0、2.0、1.1、1.0
.NET Compact Framework
サポート対象 : 3.5、2.0、1.0
XNA Framework
サポート対象 : 2.0、1.0
参照
その他の技術情報