ObjectDisposedException 클래스
정의
삭제된 개체에서 연산이 수행될 때 throw되는 예외입니다.The exception that is thrown when an operation is performed on a disposed object.
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
- 상속
- 상속
- 특성
예제
다음 예제에서는 예외를 throw 하는 오류를 보여 줍니다 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.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
이 코드의 결과는 다음과 같습니다.This code produces the following output:
Caught:
Cannot access a closed Stream.
설명
ObjectDisposedException인터페이스를 구현 하는 개체의 멤버에 액세스 하려고 하는데 해당 개체가 삭제 된 경우이 throw 됩니다 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'sDispose
method, and you're trying to access an instance member that gets or sets the object's state. 다음 예제에서는 ObjectDisposedException 메서드를 호출한 후 타이머 알림의 빈도를 다시 설정 하려고 할 때 throw 되는을 보여 줍니다 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'sClose
method, and you're trying to access an instance member that gets or sets the object's state. 일반적으로Close
메서드는 메서드의 형식의 공용 구현을 제공 IDisposable.Dispose 합니다.Often, theClose
method provides a type's public implementation of the IDisposable.Dispose method.개체의
Dispose
메서드를 여러 번 호출 했습니다.You've called an object'sDispose
method multiple times. 일반적으로 예외를 throw 하지 않습니다.Typically, this doesn't throw an exception. 그러나 형식이 구현 하는 방법에 따라 IDisposable.Dispose 에 대 한 여러 호출을 허용 하지 않을 수 있습니다Dispose
.However, depending on how a type implements IDisposable.Dispose, it may not allow multiple calls toDispose
.
대부분의 경우이 예외는 개발자 오류에서 발생 합니다.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) |
serialize된 데이터를 사용하여 ObjectDisposedException 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the ObjectDisposedException class with serialized data. |
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 클래스의 새 인스턴스를 초기화합니다.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 클래스의 새 인스턴스를 초기화합니다.Initializes a new instance of the ObjectDisposedException class with the specified object name and message. |
속성
Data |
예외에 대한 사용자 정의 정보를 추가로 제공하는 키/값 쌍 컬렉션을 가져옵니다.Gets a collection of key/value pairs that provide additional user-defined information about the exception. (다음에서 상속됨 Exception) |
HelpLink |
이 예외와 연결된 도움말 파일에 대한 링크를 가져오거나 설정합니다.Gets or sets a link to the help file associated with this exception. (다음에서 상속됨 Exception) |
HResult |
특정 예외에 할당된 코드화된 숫자 값인 HRESULT를 가져오거나 설정합니다.Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception. (다음에서 상속됨 Exception) |
InnerException |
현재 예외를 발생시킨 Exception 인스턴스를 가져옵니다.Gets the Exception instance that caused the current exception. (다음에서 상속됨 Exception) |
Message |
오류를 설명하는 메시지를 가져옵니다.Gets the message that describes the error. |
ObjectName |
삭제된 개체의 이름을 가져옵니다.Gets the name of the disposed object. |
Source |
오류를 발생시키는 애플리케이션 또는 개체의 이름을 가져오거나 설정합니다.Gets or sets the name of the application or the object that causes the error. (다음에서 상속됨 Exception) |
StackTrace |
호출 스택의 직접 실행 프레임 문자열 표현을 가져옵니다.Gets a string representation of the immediate frames on the call stack. (다음에서 상속됨 Exception) |
TargetSite |
현재 예외를 throw하는 메서드를 가져옵니다.Gets the method that throws the current exception. (다음에서 상속됨 Exception) |
메서드
Equals(Object) |
지정된 개체가 현재 개체와 같은지 확인합니다.Determines whether the specified object is equal to the current object. (다음에서 상속됨 Object) |
GetBaseException() |
파생 클래스에서 재정의된 경우 하나 이상의 후속 예외의 근본 원인이 되는 Exception 을 반환합니다.When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions. (다음에서 상속됨 Exception) |
GetHashCode() |
기본 해시 함수로 작동합니다.Serves as the default hash function. (다음에서 상속됨 Object) |
GetObjectData(SerializationInfo, StreamingContext) |
매개 변수 이름 및 추가 예외 정보를 사용하여 SerializationInfo 개체를 검색합니다.Retrieves the SerializationInfo object with the parameter name and additional exception information. |
GetObjectData(SerializationInfo, StreamingContext) |
파생 클래스에서 재정의된 경우 예외에 관한 정보를 SerializationInfo 에 설정합니다.When overridden in a derived class, sets the SerializationInfo with information about the exception. (다음에서 상속됨 Exception) |
GetType() |
현재 인스턴스의 런타임 형식을 가져옵니다.Gets the runtime type of the current instance. (다음에서 상속됨 Exception) |
MemberwiseClone() |
현재 Object의 단순 복사본을 만듭니다.Creates a shallow copy of the current Object. (다음에서 상속됨 Object) |
ToString() |
현재 예외에 대한 문자열 표현을 만들고 반환합니다.Creates and returns a string representation of the current exception. (다음에서 상속됨 Exception) |
이벤트
SerializeObjectState |
예외에 대한 serialize된 데이터가 들어 있는 예외 상태 개체가 만들어지도록 예외가 serialize될 때 발생합니다.Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception. (다음에서 상속됨 Exception) |