Export (0) Print
Expand All

ObjectDisposedException Class

The exception that is thrown when an operation is performed on a disposed object.

For a list of all members of this type, see ObjectDisposedException Members.

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

[Visual Basic]
<Serializable>
Public Class ObjectDisposedException
   Inherits InvalidOperationException
[C#]
[Serializable]
public class ObjectDisposedException : InvalidOperationException
[C++]
[Serializable]
public __gc class ObjectDisposedException : public
   InvalidOperationException
[JScript]
public
   Serializable
class ObjectDisposedException extends
   InvalidOperationException

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Example

[Visual Basic, C#, C++] The following example demonstrates an error that causes the ObjectDisposedException exception to be thrown.

[Visual Basic] 
Imports System
Imports System.IO

Public Class ObjectDisposedExceptionTest
   
   Public Shared Sub Main()
      Dim ms As New MemoryStream(16)
      ms.Close()
      Try
         ms.ReadByte()
      Catch e As ObjectDisposedException
         Console.WriteLine("Caught: {0}", e.Message)
      End Try
   End Sub 'Main
End Class 'ObjectDisposedExceptionTest

[C#] 
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);
      }
   }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {     
    MemoryStream* ms = new MemoryStream(16);
    ms->Close();
    try {
        ms->ReadByte();
    } catch (ObjectDisposedException* e) {
        Console::WriteLine(S"Caught: {0}", e->Message);
    }
}

[Visual Basic, C#, C++] This code produces the following output:

[Visual Basic, C#, C++] Caught:

Cannot access a closed Stream.

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

ObjectDisposedException Members | System Namespace | IDisposable

Show:
© 2014 Microsoft