System 名前空間


.NET Framework クラス ライブラリ
ArgumentException クラス

更新 : 2007 年 11 月

メソッドに渡された引数のいずれかが無効な場合にスローされる例外。

名前空間 :  System
アセンブリ :  mscorlib (mscorlib.dll 内)
構文

Visual Basic (宣言)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class ArgumentException _
    Inherits SystemException _
    Implements ISerializable
Visual Basic (使用法)
Dim instance As ArgumentException
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class ArgumentException : SystemException, 
    ISerializable
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class ArgumentException : public SystemException, 
    ISerializable
J#
/** @attribute SerializableAttribute */ 
/** @attribute ComVisibleAttribute(true) */
public class ArgumentException extends SystemException implements ISerializable
JScript
public class ArgumentException extends SystemException implements ISerializable
解説

ArgumentException は、メソッドの呼び出し時に渡された引数の少なくとも 1 つが、呼び出されたメソッドのパラメータの仕様に一致していないとスローされます。ArgumentException のすべてのインスタンスは、無効な引数と、その引数に指定できる有効な値の範囲を説明する、わかりやすいエラーメッセージを保持する必要があります。

ArgumentException の主要な派生クラスは、ArgumentNullExceptionArgumentOutOfRangeException です。これらの派生クラスは、両方とも使用できない場合以外は、ArgumentException の代わりに使用されます。たとえば、次の場合は、それぞれの派生クラスで例外をスローします。

  • nullNothingnullptrnull 参照 (Visual Basic では Nothing) を有効な引数として受け付けないメソッドに null 値が渡された場合は、必ず ArgumentNullException を使用します。

  • 指定できる範囲を外れた値が引数として渡された場合は、ArgumentOutOfRangeException を使用します。たとえば、DateTime の作成時に、月を指定する引数として "46" という値が渡された場合が該当します。

引数を指定せずにメソッドが呼び出された場合、引数が原因ではないエラーが発生している場合は、InvalidOperationException を使用します。

ArgumentException は、値 0x80070057 を保持する HRESULT COR_E_ARGUMENT を使用します。

ArgumentException のインスタンスの初期プロパティ値の一覧については、ArgumentException コンストラクタのトピックを参照してください。


ArgumentException をスローし、キャッチする方法を次の例に示します。

Visual Basic
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.
C#
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.
Visual C++
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
参照

参照

その他の技術情報

タグ :


Page view tracker