Mono Class Library: System Namespace

System.InvalidOperationException Class

Represents the error that occurs when an operation cannot be performed. [Edit]

See Also: InvalidOperationException Members

System.Object
     System.Exception
          System.SystemException
               System.InvalidOperationException

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

Thread Safety

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

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.

[Edit]

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