public class ObjectDisposedException : InvalidOperationException
Object
Exception
SystemException
InvalidOperationException
ObjectDisposedException
mscorlib
BCL
Represents the error that occurs when an operation is performed on a disposed object.
[Note: For additional information about disposing objects, see the IDisposable interface.]
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.
System Namespace
ObjectDisposedException Constructors
ObjectDisposedException(System.String) Constructor
ObjectDisposedException(System.String, System.String) Constructor
ObjectDisposedException Properties
ObjectDisposedException.Message Property
ObjectDisposedException.ObjectName Property
public ObjectDisposedException(string objectName);
Constructs and initializes a new instance of the ObjectDisposedException class.
- objectName
- A String containing the name of the disposed object.
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 errorand 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.]
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".
System.ObjectDisposedException Class, System Namespace
public ObjectDisposedException(string objectName, string message);
Constructs and initializes a new instance of the ObjectDisposedException class.
This constructor initializes the System.ObjectDisposedException.Message property of the new instance using message, and the System.ObjectDisposedException.ObjectName property using objectName . If message isnull, 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.
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".
System.ObjectDisposedException Class, System Namespace
public override string Message { get; }
Gets the message that describes the error.
A String that describes the error.
If the System.ObjectDisposedException.ObjectName property is notnull, the message includes the name of the object.This property is read-only.
[Note: This property overrides System.Exception.Message.]
System.ObjectDisposedException Class, System Namespace
public string ObjectName { get; }
Gets the name of the disposed object.
A String containing the name of the disposed object.
[Note: If this property is notnullor 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.
System.ObjectDisposedException Class, System Namespace