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 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介面的物件成員,而該物件已被丟棄時,會拋出 An IAsyncDisposable 鍵。 通常,此例外由以下條件之一引起:

  • 你已經呼叫了一個 IDisposable 物件的方法 Dispose (或物件 IDisposableAsync 的方法 DisposeAsync ),並且你正在嘗試存取一個實例成員,該成員會取得或設定該物件的狀態。 以下範例說明 ObjectDisposedException 了當你呼叫 Timer.Dispose 該方法後,嘗試重設計時器通知頻率時會拋出的 that。

    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.DisposeCloseAsync<xref:System.IAsyncDisposable.DisposeAsync*?displayProperty=nameWithType>亦然。

  • 你已經多次呼叫一個物件或DisposeDisposeAsync方法。 通常這不會造成例外。 然而,根據型別如何實 IDisposable.Dispose 作 or IAsyncDisposable.DisposeAsync,可能不允許對該方法進行多次呼叫。

大多數情況下,此例外是開發者錯誤所致。 你應該不要在區 try/catch 塊中處理錯誤,而是應該修正錯誤,通常是透過重新實例化物件來實現。

建構函式

名稱 Description
ObjectDisposedException(SerializationInfo, StreamingContext)
已淘汰.

初始化一個新的類別實例 ObjectDisposedException ,並使用序列化資料。

ObjectDisposedException(String, Exception)

初始化類別的新實例 ObjectDisposedException ,並附上指定的錯誤訊息及導致該異常的內部例外的參考。

ObjectDisposedException(String, String)

使用指定的物件名稱和訊息,初始化 ObjectDisposedException 類別的新實例。

ObjectDisposedException(String)

使用包含已處置物件名稱的字串,初始化 ObjectDisposedException 類別的新實例。

屬性

名稱 Description
Data

取得一組鍵值對,提供關於例外的額外使用者定義資訊。

(繼承來源 Exception)
HelpLink

取得或設定與此例外相關的說明檔案連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,一個編碼的數值,指派給特定例外。

(繼承來源 Exception)
InnerException

會取得 Exception 造成目前例外的實例。

(繼承來源 Exception)
Message

會收到描述錯誤的訊息。

ObjectName

會得到被丟棄物品的名稱。

Source

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

(繼承來源 Exception)
StackTrace

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

(繼承來源 Exception)
TargetSite

會取得拋出當前例外的方法。

(繼承來源 Exception)

方法

名稱 Description
Equals(Object)

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

(繼承來源 Object)
GetBaseException()

當在派生類別中被覆寫時,回傳 Exception 是一個或多個後續例外的根因。

(繼承來源 Exception)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

取得帶有參數名稱及額外例外資訊的 SerializationInfo 物件。

GetType()

取得目前實例的執行時型態。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object的淺層複本。

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

若指定ObjectDisposedExceptioncondition,則拋出 。true

ThrowIf(Boolean, Type)

若指定ObjectDisposedExceptioncondition,則拋出 。true

ToString()

建立並回傳當前例外的字串表示。

(繼承來源 Exception)

事件

名稱 Description
SerializeObjectState
已淘汰.

當例外被序列化以建立包含該例外序列化資料的例外狀態物件時,會發生這種情況。

(繼承來源 Exception)

適用於

另請參閱