System.InvalidOperationException Class

public class InvalidOperationException : SystemException

Base Types

Object
  Exception
    SystemException
      InvalidOperationException

Assembly

mscorlib

Library

BCL

Summary

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

Description

[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.

]

Example

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

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()

See Also

System Namespace

Members

InvalidOperationException Constructors

InvalidOperationException() Constructor
InvalidOperationException(System.String) Constructor
InvalidOperationException(System.String, System.Exception) Constructor


InvalidOperationException() Constructor

public InvalidOperationException();

Summary

Constructs and initializes a new instance of the InvalidOperationException class.

Description

This constructor initializes the System.InvalidOperationException.Message property of the new instance to a system-supplied message that describes the error, such as "The requested operation cannot be performed." This message takes into account the current system culture.

The System.InvalidOperationException.InnerException property is initialized to null .

See Also

System.InvalidOperationException Class, System Namespace

InvalidOperationException(System.String) Constructor

public InvalidOperationException(string message);

Summary

Constructs and initializes a new instance of the InvalidOperationException class.

Parameters

message
A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.

Description

This constructor initializes the System.InvalidOperationException.Message property of the new instance using message. If message is null , the System.InvalidOperationException.Message property is initialized to a system-supplied message. The System.InvalidOperationException.InnerException property is initialized to null .

See Also

System.InvalidOperationException Class, System Namespace

InvalidOperationException(System.String, System.Exception) Constructor

public InvalidOperationException(string message, Exception innerException);

Summary

Constructs and initializes a new instance of the InvalidOperationException class.

Parameters

message
A String that describes the error. The content of message is intended to be understood by humans. The caller of this constructor is required to ensure that this string has been localized for the current system culture.
innerException
An instance of Exception that is the cause of the current Exception. If innerException is non-null, then the current Exception was raised in a catch block handling innerException .

Description

This constructor initializes the System.InvalidOperationException.Message property of the new instance using message, and the System.InvalidOperationException.InnerException property using innerException. If message is null , the System.InvalidOperationException.Message property is initialized to a system-supplied message.

[Note: For information on inner exceptions, see System.Exception.InnerException .]

See Also

System.InvalidOperationException Class, System Namespace