Mono Class Library: System.ObjectDisposedException Overview | Members

System.ObjectDisposedException Constructor

Constructs and initializes a new instance of the ObjectDisposedException class. [Edit]

public ObjectDisposedException (string objectName)

Parameters

objectName
A string containing the name of the disposed object. [Edit]

Remarks

This constructor initializes the ObjectDisposedException.ObjectName property of the new instance using objectName. The 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 ObjectDisposedException.InnerException property of the new instance is initialized to null.

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

[Edit]

Example

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

C# Example
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".

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0