ObjectDisposedException ObjectDisposedException ObjectDisposedException ObjectDisposedException Class

定義

當作業在處置物件上執行時所擲回的例外狀況。The exception that is thrown when an operation is performed on a disposed object.

public ref class ObjectDisposedException : InvalidOperationException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class ObjectDisposedException : InvalidOperationException
type ObjectDisposedException = class
    inherit InvalidOperationException
Public Class ObjectDisposedException
Inherits InvalidOperationException
繼承
屬性

範例

下列範例示範會導致錯誤ObjectDisposedException擲回的例外狀況。The following example demonstrates an error that causes the ObjectDisposedException exception to be thrown.

using namespace System;
using namespace System::IO;
int main()
{
   MemoryStream^ ms = gcnew MemoryStream( 16 );
   ms->Close();
   try
   {
      ms->ReadByte();
   }
   catch ( ObjectDisposedException^ e ) 
   {
      Console::WriteLine( "Caught: {0}", e->Message );
   }

}

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);
      }
   }
}
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

此程式碼會產生下列輸出:This code produces the following output:

Caught:  
  Cannot access a closed Stream.  

備註

ObjectDisposedException當您嘗試存取該物件會實作的成員時,會擲回IDisposable介面,而該物件已經過處置。An ObjectDisposedException is thrown when you try to access a member of an object that implements the IDisposable interface, and that object has been disposed. 一般而言,此例外狀況被因下列情況之一:Typically, this exception is caused by one of the following conditions:

  • 您已呼叫物件的Dispose方法,而且您正在嘗試存取執行個體成員,取得或設定物件的狀態。You've called an object's Dispose method, and you're trying to access an instance member that gets or sets the object's state. 下列範例說明ObjectDisposedException當您嘗試重設的計時器通知的頻率之後您呼叫, 時擲回Timer.Dispose方法。The following example illustrates the ObjectDisposedException that is thrown when you try to reset the frequency of timer notifications after you call the Timer.Dispose method.

    using System;
    using System.Threading;
    
    public class Example
    {
       public static void Main()
       {
          Timer t = new Timer(TimerNotification, null, 
                             100, Timeout.Infinite);
          Thread.Sleep(2000);
          t.Dispose();
          
          t.Change(200, 1000);                   
          Thread.Sleep(3000);
       }
    
       private static void TimerNotification(Object obj)
       {
          Console.WriteLine("Timer event fired at {0:F}", DateTime.Now);
       }
    }
    // The example displays output like the following:
    //    Timer event fired at Monday, July 14, 2014 11:54:08 AM
    //    
    //    Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object.
    //       at System.Threading.TimerQueueTimer.Change(UInt32 dueTime, UInt32 period)
    //       at Example.Main()
    
    Imports System.Threading
    
    Module Example
       Public Sub Main()
          Dim t As New Timer(AddressOf TimerNotification, Nothing, 
                             100, Timeout.Infinite)
          Thread.Sleep(2000)
          t.Dispose()
          
          t.Change(200, 1000)                   
          Thread.Sleep(3000)
       End Sub
    
       Private Sub TimerNotification(obj As Object)
          Console.WriteLine("Timer event fired at {0:F}", Date.Now)
       End Sub
    End Module
    ' The example displays output like the following:
    '    Timer event fired at Monday, July 14, 2014 11:54:08 AM
    '    
    '    Unhandled Exception: System.ObjectDisposedException: Cannot access a disposed object.
    '       at System.Threading.TimerQueueTimer.Change(UInt32 dueTime, UInt32 period)
    '       at Example.Main()
    
  • 您已呼叫物件的Close方法,而且您正在嘗試存取執行個體成員,取得或設定物件的狀態。You've called an object's Close method, and you're trying to access an instance member that gets or sets the object's state. 通常Close方法會提供類型的公用實作IDisposable.Dispose方法。Often, the Close method provides a type's public implementation of the IDisposable.Dispose method.

  • 您已呼叫物件的Dispose方法多次。You've called an object's Dispose method multiple times. 一般而言,這不會擲回例外狀況。Typically, this doesn't throw an exception. 不過,根據型別實作的如何IDisposable.Dispose,它可能不允許多個呼叫DisposeHowever, depending on how a type implements IDisposable.Dispose, it may not allow multiple calls to Dispose.

在大部分情況下,此例外狀況會造成從開發人員錯誤。In most cases, this exception results from developer error. 而不是處理中的錯誤try / catch區塊中,您應該要更正錯誤,通常,重新具現化物件。Instead of handling the error in a try/catch block, you should correct the error, typically by reinstantiating the object.

建構函式

ObjectDisposedException(SerializationInfo, StreamingContext) ObjectDisposedException(SerializationInfo, StreamingContext) ObjectDisposedException(SerializationInfo, StreamingContext) ObjectDisposedException(SerializationInfo, StreamingContext)

使用序列化資料,初始化 ObjectDisposedException 類別的新執行個體。Initializes a new instance of the ObjectDisposedException class with serialized data.

ObjectDisposedException(String) ObjectDisposedException(String) ObjectDisposedException(String) ObjectDisposedException(String)

使用含有處置物件的名稱來初始化 ObjectDisposedException 類別的新執行個體。Initializes a new instance of the ObjectDisposedException class with a string containing the name of the disposed object.

ObjectDisposedException(String, Exception) ObjectDisposedException(String, Exception) ObjectDisposedException(String, Exception) ObjectDisposedException(String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 ObjectDisposedException 類別的新執行個體。Initializes a new instance of the ObjectDisposedException class with a specified error message and a reference to the inner exception that is the cause of this exception.

ObjectDisposedException(String, String) ObjectDisposedException(String, String) ObjectDisposedException(String, String) ObjectDisposedException(String, String)

使用指定的物件名稱和訊息來初始化 ObjectDisposedException 類別的新執行個體。Initializes a new instance of the ObjectDisposedException class with the specified object name and message.

屬性

Data Data Data Data

取得提供例外狀況之其他使用者定義相關資訊的索引鍵/值組集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
HelpLink HelpLink HelpLink HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult HResult HResult HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException InnerException InnerException InnerException

取得造成目前例外狀況的 Exception 執行個體。Gets the Exception instance that caused the current exception.

(Inherited from Exception)
Message Message Message Message

取得描述錯誤的訊息。Gets the message that describes the error.

ObjectName ObjectName ObjectName ObjectName

取得處置物件的名稱。Gets the name of the disposed object.

Source Source Source Source

取得或設定造成錯誤的應用程式或物件的名稱。Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception)
StackTrace StackTrace StackTrace StackTrace

取得呼叫堆疊上即時運算框架的字串表示。Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite TargetSite TargetSite TargetSite

取得擲回目前例外狀況的方法。Gets the method that throws the current exception.

(Inherited from Exception)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException() GetBaseException() GetBaseException() GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

擷取具有參數名稱和額外例外狀況資訊的 SerializationInfo 物件。Retrieves the SerializationInfo object with the parameter name and additional exception information.

GetType() GetType() GetType() GetType()

取得目前執行個體的執行階段類型。Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

建立並傳回目前例外狀況的字串表示。Creates and returns a string representation of the current exception.

(Inherited from Exception)

事件

SerializeObjectState SerializeObjectState SerializeObjectState SerializeObjectState

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception)

適用於

另請參閱