System.InvalidOperationException Class
Represents the error that occurs when an operation cannot be performed.

See Also: InvalidOperationException Members

Syntax

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

Remarks

Note:

InvalidOperationException is typically thrown when the state of one or more objects determines whether an operation can be performed.

The InvalidOperationException exception should not be thrown for errors caused by invalid arguments. For invalid argument errors, throw ArgumentException or one of its derived types, such as ArgumentNullException or ArgumentOutOfRangeException.

The ldflda CIL instruction throws InvalidOperationException.

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