Export (0) Print
Expand All

InvalidEnumArgumentException Class

The exception thrown when using invalid arguments that are enumerators.

For a list of all members of this type, see InvalidEnumArgumentException Members.

System.Object
   System.Exception
      System.SystemException
         System.ArgumentException
            System.ComponentModel.InvalidEnumArgumentException

[Visual Basic]
Public Class InvalidEnumArgumentException
   Inherits ArgumentException
[C#]
public class InvalidEnumArgumentException : ArgumentException
[C++]
public __gc class InvalidEnumArgumentException : public
   ArgumentException
[JScript]
public class InvalidEnumArgumentException extends ArgumentException

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

This exception is thrown if you pass an invalid enumeration value to a method or when setting a property.

Example

[Visual Basic, C#, C++] The following example shows how to catch an InvalidEnumArgumentException exception and interpret its content. The example attempts to pass an invalid enumeration value (MessageBoxButtons) through casting, as the MessageBox.Show method's third argument. Upon catching the exception, the example fetches the respective error message, the invalid parameter, stack trace, and origin of the exception.

[Visual Basic] 
Try
    ' Attempts to pass an invalid enum value (MessageBoxButtons) to the Show method
    Dim myButton As MessageBoxButtons
    myButton = CType(123, MessageBoxButtons)
    MessageBox.Show("This is a message", "This is the Caption", myButton)
Catch invE As System.ComponentModel.InvalidEnumArgumentException
    Console.WriteLine(invE.Message)
    Console.WriteLine(invE.ParamName)
    Console.WriteLine(invE.StackTrace)
    Console.WriteLine(invE.Source)
End Try

[C#] 
try 
{
// Attempts to pass an invalid enum value (MessageBoxButtons) to the Show method
    MessageBoxButtons myButton= (MessageBoxButtons) 123;
    MessageBox.Show("This is a message","This is the Caption",myButton);
}
catch(InvalidEnumArgumentException invE) 
{
    Console.WriteLine(invE.Message);
    Console.WriteLine(invE.ParamName);
    Console.WriteLine(invE.StackTrace);
    Console.WriteLine(invE.Source);
}

[C++] 
try 
{
//Attempting to pass an invalid enum value (MessageBoxButtons) to the Show method
MessageBoxButtons myButton = (MessageBoxButtons) 123;    // to fix use System::Windows::Forms::DialogResult::OK;
MessageBox::Show(this, "This is a message","This is the Caption", myButton);
}
catch(InvalidEnumArgumentException* invE) 
{
    Console::WriteLine(invE->Message);
    Console::WriteLine(invE->ParamName);
    Console::WriteLine(invE->StackTrace);
    Console::WriteLine(invE->Source);
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.ComponentModel

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System (in System.dll)

See Also

InvalidEnumArgumentException Members | System.ComponentModel Namespace

Show:
© 2014 Microsoft