System.ObjectDisposedException Class

public class ObjectDisposedException : InvalidOperationException

Base Types

Object
  Exception
    SystemException
      InvalidOperationException
        ObjectDisposedException

Assembly

mscorlib

Library

BCL

Summary

Represents the error that occurs when an operation is performed on a disposed object.

Description

[Note: For additional information about disposing objects, see the IDisposable interface.]

Example

The following example demonstrates an error that causes the ObjectDisposedException exception to be thrown.

using System;
using System.IO;

public class ObjectDisposedExceptionTest {
   public static void Main() {     
      MemoryStream ms = new MemoryStream(16);
      ms.Close();
      try {
         ms.ReadByte();
      } 
      catch (ObjectDisposedException e) {
         Console.WriteLine("Caught: {0}", e.Message);
      }
   }
}
The output is

Caught: Cannot access a closed Stream.

See Also

System Namespace

Members

ObjectDisposedException Constructors

ObjectDisposedException(System.String) Constructor
ObjectDisposedException(System.String, System.String) Constructor

ObjectDisposedException Properties

ObjectDisposedException.Message Property
ObjectDisposedException.ObjectName Property


ObjectDisposedException(System.String) Constructor

public ObjectDisposedException(string objectName);

Summary

Constructs and initializes a new instance of the ObjectDisposedException class.

Parameters

objectName
A String containing the name of the disposed object.

Description

This constructor initializes the System.ObjectDisposedException.ObjectName property of the new instance using objectName. The System.ObjectDisposedException.Message property is initialized to a system-supplied message that describes the error and includes objectname . This message takes into account the current system culture.

The System.ObjectDisposedException.InnerException property of the new instance is initialized to null .

[Note: If objectName is null , the System.ObjectDisposedException.Message property contains only an error message.]

Example

The following example displays the error message of a ObjectDisposedException instance created using this constructor.

using System;

public class ExampleDisposableObject : IDisposable {
 public static void Main() {
 
 ExampleDisposableObject obj = new ExampleDisposableObject();
 
 obj.Close();
 
 try {
 Console.WriteLine(obj);
 } catch (ObjectDisposedException e) {
 Console.WriteLine("Caught: {0}", e.Message);
 }
 }
 

 public ExampleDisposableObject() {
 isDisposed = false;
 }
 
 ~ExampleDisposableObject() {
 Dispose(true);
 }
 
 public void Close() {
 Dispose(true);
 }
 
 public void Dispose() {
 Dispose(true);
 }
 
 public void Dispose(bool disposing) {
 isDisposed = true;
 }
 
 public override String ToString() {
 if(isDisposed)
 throw new ObjectDisposedException("ExampleDisposableObject");
 else
 return "This is an instance of ExampleDisposableObject.";
 }
 
 private bool isDisposed;
}
   
The output is

Caught: Cannot access a disposed object named "ExampleDisposableObject".

Object name: "ExampleDisposableObject".

See Also

System.ObjectDisposedException Class, System Namespace

ObjectDisposedException(System.String, System.String) Constructor

public ObjectDisposedException(string objectName, string message);

Summary

Constructs and initializes a new instance of the ObjectDisposedException class.

Parameters

objectName
A String containing the name of the disposed object.
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.ObjectDisposedException.Message property of the new instance using message, and the System.ObjectDisposedException.ObjectName property using objectName . If message is null , the System.ObjectDisposedException.Message property is initialized to the system-supplied message provided by the constructor that takes no arguments.

The System.ObjectDisposedException.InnerException property of the new instance is initialized to null .

Example

The following example throws a ObjectDisposedException instance created using this constructor.

using System;

public class ExampleDisposableObject : IDisposable {
 public static void Main() {
 
 ExampleDisposableObject obj = new ExampleDisposableObject();
 
 obj.Close();
 
 try {
 Console.WriteLine(obj);
 } catch (ObjectDisposedException e) {
 Console.WriteLine("Caught: {0}", e.Message);
 }
 }
 

 public ExampleDisposableObject() {
 isDisposed = false;
 }
 
 ~ExampleDisposableObject() {
 Dispose(true);
 }
 
 public void Close() {
 Dispose(true);
 }
 
 public void Dispose() {
 Dispose(true);
 }
 
 public void Dispose(bool disposing) {
 isDisposed = true;
 }
 
 public override String ToString() {
 if(isDisposed) {
 string message = "Oh-oh! This object has been disposed!";
 string objectName = "ExampleDisposableObject"; 
 throw new ObjectDisposedException(objectName, message);
 }
 else
 return "Hello, World!";
 }
 
 private bool isDisposed;
}
The output is

Caught: Oh-oh! This object has been disposed!

Object name: "ExampleDisposableObject".

See Also

System.ObjectDisposedException Class, System Namespace

ObjectDisposedException.Message Property

public override string Message { get; }

Summary

Gets the message that describes the error.

Property Value

A String that describes the error.

Description

If the System.ObjectDisposedException.ObjectName property is not null , the message includes the name of the object.

This property is read-only.

[Note: This property overrides System.Exception.Message.]

See Also

System.ObjectDisposedException Class, System Namespace

ObjectDisposedException.ObjectName Property

public string ObjectName { get; }

Summary

Gets the name of the disposed object.

Property Value

A String containing the name of the disposed object.

Description

[Note: If this property is not null or System.String.Empty , the value of this property is included in the string returned by the System.ObjectDisposedException.Message property. ]

This property is read-only.

See Also

System.ObjectDisposedException Class, System Namespace