AccessViolationException 類別


嘗試讀取或寫入受保護的記憶體時所擲回的例外狀況。The exception that is thrown when there is an attempt to read or write protected memory.

public ref class AccessViolationException : SystemException
public class AccessViolationException : SystemException
type AccessViolationException = class
    inherit SystemException
Public Class AccessViolationException
Inherits SystemException


當程式碼嘗試讀取或寫入尚未配置或沒有存取權的記憶體時,不受管理或不安全的程式碼會發生存取違規。An access violation occurs in unmanaged or unsafe code when the code attempts to read or write to memory that has not been allocated, or to which it does not have access. 這通常是因為指標的值不正確所造成。This usually occurs because a pointer has a bad value. 並非所有透過錯誤指標讀取或寫入的動作都會導致存取違規,因此存取違規通常指出已透過錯誤指標執行多次讀取或寫入,且該記憶體可能已毀損。Not all reads or writes through bad pointers lead to access violations, so an access violation usually indicates that several reads or writes have occurred through bad pointers, and that memory might be corrupted. 因此,存取違規幾乎一律會指出嚴重的程式設計錯誤。Thus, access violations almost always indicate serious programming errors. AccessViolationException 清楚地識別出這些嚴重的錯誤。An AccessViolationException clearly identifies these serious errors.

在完全由可驗證的受控碼組成的程式中,所有參考都是有效或 null,而且不可能發生存取違規。In programs consisting entirely of verifiable managed code, all references are either valid or null, and access violations are impossible. 嘗試在可驗證程式代碼中參考 null 參考的任何作業,都會擲回 NullReferenceException 例外狀況。Any operation that attempts to reference a null reference in verifiable code throws a NullReferenceException exception. 只有當可驗證的 managed 程式碼與非受控碼或不安全的 managed 程式碼互動時,才會發生 AccessViolationExceptionAn AccessViolationException occurs only when verifiable managed code interacts with unmanaged code or with unsafe managed code.

AccessViolationException 例外狀況疑難排解Troubleshooting AccessViolationException exceptions

AccessViolationException 例外狀況只會發生在不安全的 managed 程式碼中,或可驗證的 managed 程式碼與非受控碼互動時:An AccessViolationException exception can occur only in unsafe managed code or when verifiable managed code interacts with unmanaged code:

不論是哪一種情況,您都可以識別並更正 AccessViolationException 例外狀況的原因,如下所示:In either case, you can identify and correct the cause of the AccessViolationException exception as follows:

請確定已配置您嘗試存取的記憶體。Make sure that the memory that you are attempting to access has been allocated.
嘗試存取受保護的記憶體時,一律會擲回 AccessViolationException 例外狀況,也就是存取未配置或未由進程所擁有的記憶體。An AccessViolationException exception is always thrown by an attempt to access protected memory -- that is, to access memory that is not allocated or that is not owned by a process.

自動記憶體管理是 Common Language Runtime (CLR) 提供的一項服務。Automatic memory management is one of the services that the common language runtime provides. 如果 managed 程式碼提供與您的非受控碼相同的功能,您可能會想要移至 managed 程式碼,以利用這項功能。If managed code provides the same functionality as your unmanaged code, you may wish to move to managed code to take advantage of this functionality. 如需詳細資訊,請參閱 Automatic Memory ManagementFor more information, see Automatic Memory Management.

請確定您嘗試存取的記憶體並未損毀。Make sure that the memory that you are attempting to access has not been corrupted.
如果透過損壞的指標進行數次讀取或寫入作業,可能會損毀記憶體。If several read or write operations have occurred through bad pointers, memory may be corrupted. 這通常會在讀取或寫入至預先定義緩衝區以外的位址時發生。This typically occurs when reading or writing to addresses outside of a predefined buffer.

AccessViolationException 和 try/catch 區塊AccessViolationException and try/catch blocks

.NET Framework 4.NET Framework 4開始,如果例外狀況發生在通用語言執行平臺所保留的記憶體之外,則不會由結構化例外狀況處理常式中的 catch 語句處理 common language runtime 所擲回的 AccessViolationException 例外狀況。Starting with the .NET Framework 4.NET Framework 4, AccessViolationException exceptions thrown by the common language runtime are not handled by the catch statement in a structured exception handler if the exception occurs outside of the memory reserved by the common language runtime. 若要處理這類 AccessViolationException 例外狀況,您應該將 HandleProcessCorruptedStateExceptionsAttribute 屬性套用至擲回例外狀況的方法。To handle such an AccessViolationException exception, you should apply the HandleProcessCorruptedStateExceptionsAttribute attribute to the method in which the exception is thrown. 這項變更不會影響使用者程式碼所擲回 AccessViolationException 例外狀況,而這可能會繼續由 catch 的語句攔截到。This change does not affect AccessViolationException exceptions thrown by user code, which can continue to be caught by a catch statement. 針對針對舊版所撰寫的程式碼,而您想要重新編譯並執行而不修改 .NET Framework 4.NET Framework 4上的 .NET Framework,您可以將<legacyCorruptedStateExceptionsPolicy >專案新增至應用程式的設定檔。For code written for previous versions of the .NET Framework that you want to recompile and run without modification on the .NET Framework 4.NET Framework 4, you can add the <legacyCorruptedStateExceptionsPolicy> element to your app's configuration file. 請注意,如果您已定義 AppDomain.FirstChanceExceptionAppDomain.UnhandledException 事件的處理常式,也可以收到例外狀況的通知。Note that you can also receive notification of the exceptions if you have defined a handler for the AppDomain.FirstChanceException or AppDomain.UnhandledException event.

版本資訊Version Information

在 .NET Framework 2.0 和更新版本中,會擲回這個例外狀況。This exception is thrown in the .NET Framework 2.0 and later versions. 在舊版的 .NET Framework 中,非受控碼或不安全的 managed 程式碼中的存取違規,是由 managed 程式碼中的 NullReferenceException 表示。In earlier versions of the .NET Framework, an access violation in unmanaged code or unsafe managed code is represented by a NullReferenceException in managed code. 當可驗證的 managed 程式碼中的 null 參考被取值、不牽涉到資料損毀的情況,而且無法區分版本1.0 或1.1 中的兩種情況時,也會擲回 NullReferenceExceptionA NullReferenceException is also thrown when a null reference is dereferenced in verifiable managed code, an occurrence that does not involve data corruption, and there is no way to distinguish between the two situations in versions 1.0 or 1.1.

系統管理員可以允許選取的應用程式還原為 .NET Framework 版本1.1 的行為。Administrators can allow selected applications to revert to the behavior of the .NET Framework version 1.1. 將下面這一行放在應用程式佈建檔的<執行時間> 元素區段中:Place the following line in the <runtime> Element section of the configuration file for the application:

<legacyNullReferenceExceptionPolicy enabled = "1"/>  



以系統提供的錯誤說明訊息,初始化 AccessViolationException 類別的新執行個體。Initializes a new instance of the AccessViolationException class with a system-supplied message that describes the error.

AccessViolationException(SerializationInfo, StreamingContext)

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


使用指定的錯誤說明訊息,初始化 AccessViolationException 類別的新執行個體。Initializes a new instance of the AccessViolationException class with a specified message that describes the error.

AccessViolationException(String, Exception)

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



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

(繼承來源 Exception)

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

(繼承來源 Exception)

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

(繼承來源 Exception)

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

(繼承來源 Exception)

取得描述目前例外狀況的訊息。Gets a message that describes the current exception.

(繼承來源 Exception)

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

(繼承來源 Exception)

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

(繼承來源 Exception)

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

(繼承來源 Exception)



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

(繼承來源 Object)

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

(繼承來源 Exception)

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

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

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(繼承來源 Exception)

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

(繼承來源 Exception)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)

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

(繼承來源 Exception)



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

(繼承來源 Exception)