ObjectDisposedException 類別

定義

當作業在處置的物件上執行時所擲回的例外狀況。

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

範例

下列範例示範導致 ObjectDisposedException 擲回例外狀況的錯誤。

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);
      }
   }
}
open System
open System.IO

let ms = new MemoryStream 16
ms.Close()
try
    ms.ReadByte()
    |> ignore
with :? ObjectDisposedException as e ->
   printfn $"Caught: {e.Message}"
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
End Class

此程式碼會產生下列輸出:

Caught:  
  Cannot access a closed Stream.  

備註

ObjectDisposedException當您嘗試存取實 IDisposable 作介面或 IAsyncDisposable 介面的物件成員,且該物件已被處置時,就會擲回 。 一般而言,此例外狀況是由下列其中一個條件所造成:

  • 您已呼叫 IDisposable 物件的 Dispose 方法 (或物件的 DisposeAsync 方法) ,而且您嘗試存取取得或 IDisposableAsync 設定物件的狀態的實例成員。 下列範例說明 ObjectDisposedException 當您嘗試在呼叫 方法之後重設計時器通知的頻率時,所擲回的 Timer.Dispose

    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()
    
    open System
    open System.Threading
    
    let timerNotification _ =
        printfn $"Timer event fired at {DateTime.Now:F}"
    
    let t = new Timer(timerNotification, null, 100, Timeout.Infinite)
    Thread.Sleep 2000
    t.Dispose()
    
    t.Change(200, 1000)
    |> ignore
    Thread.Sleep 3000
    
    // 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 <StartupCode$fs>.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 方法,並嘗試存取取得或設定物件狀態的實例成員。 Close方法通常會提供方法的類型公用實作 IDisposable.Dispose 。 和 <xref:System.IAsyncDisposable.DisposeAsync%2A?displayProperty=nameWithType> 也是如此 CloseAsync

  • 您已多次呼叫物件的 DisposeDisposeAsync 方法。 一般而言,這不會擲回例外狀況。 不過,視類型實作 IDisposable.Dispose 的方式或 IAsyncDisposable.DisposeAsync 而定,它可能不允許對該方法進行多個呼叫。

在大部分情況下,此例外狀況會產生開發人員錯誤。 您應該藉由重新初始化 物件,而不是在區塊中 try/catch 處理錯誤。

建構函式

ObjectDisposedException(SerializationInfo, StreamingContext)

使用序列化資料,初始化 ObjectDisposedException 類別的新執行個體。

ObjectDisposedException(String)

使用含有處置物件的名稱來初始化 ObjectDisposedException 類別的新執行個體。

ObjectDisposedException(String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 ObjectDisposedException 類別的新執行個體。

ObjectDisposedException(String, String)

使用指定的物件名稱和訊息來初始化 ObjectDisposedException 類別的新執行個體。

屬性

Data

取得鍵值組的集合,這些鍵值組會提供關於例外狀況的其他使用者定義資訊。

(繼承來源 Exception)
HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。

(繼承來源 Exception)
InnerException

取得造成目前例外狀況的 Exception 執行個體。

(繼承來源 Exception)
Message

取得描述錯誤的訊息。

ObjectName

取得處置物件的名稱。

Source

取得或設定造成錯誤的應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

取得呼叫堆疊上即時運算框架的字串表示。

(繼承來源 Exception)
TargetSite

取得擲回目前例外狀況的方法。

(繼承來源 Exception)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 Exception

(繼承來源 Exception)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)

擷取具有參數名稱和額外例外狀況資訊的 SerializationInfo 物件。

GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfo

(繼承來源 Exception)
GetType()

取得目前執行個體的執行階段類型。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ThrowIf(Boolean, Object)

如果指定的 conditiontrue ,則會擲回 ObjectDisposedException

ThrowIf(Boolean, Type)

如果指定的 conditiontrue ,則會擲回 ObjectDisposedException

ToString()

建立並傳回目前例外狀況的字串表示。

(繼承來源 Exception)

事件

SerializeObjectState
已淘汰.

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。

(繼承來源 Exception)

適用於

另請參閱