System.InvalidOperationException Class

The exception that is thrown when a method call is invalid for the object's current state.

See Also: InvalidOperationException Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public class InvalidOperationException : SystemException

Remarks

InvalidOperationException is used in cases when the failure to invoke a method is caused by reasons other than invalid arguments. For example, InvalidOperationException is thrown by:

To determine the precise cause of the exception, consult the exception information for the method call that throws the exception.

If the method invocation failure is due to invalid arguments, then ArgumentException or one of its derived classes, ArgumentNullException or ArgumentOutOfRangeException, should be thrown instead.

InvalidOperationException uses the HRESULT COR_E_INVALIDOPERATION, which has the value 0x80131509.

For a list of initial property values for an instance of InvalidOperationException, see the InvalidOperationException.#ctor constructors.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Example

The following example demonstrates an error that causes a InvalidOperationException exception.

C# Example

using System;
using System.Collections;
public class InvalidOpExample  {
  public static void Main()  {
    int[] array = {0,0};
    IEnumerator enumerator = array.GetEnumerator();
    Console.Write("{0}",enumerator.Current);
  }
}
   

The output is

Unhandled Exception: System.InvalidOperationException: Enumeration has not started. Call MoveNext.
at System.SZArrayEnumerator.get_Current()
at InvalidOpExample.Main()

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0