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
public class AccessViolationException : SystemException
type AccessViolationException = class
    inherit 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. 任何尝试在可验证代码中引用空引用的操作都会引发 NullReferenceException 异常。Any operation that attempts to reference a null reference in verifiable code throws a NullReferenceException exception. AccessViolationException仅当可验证托管代码与非托管代码或不安全的托管代码进行交互时才会发生。An AccessViolationException occurs only when verifiable managed code interacts with unmanaged code or with unsafe managed code.

AccessViolationException 异常疑难解答Troubleshooting AccessViolationException exceptions

AccessViolationException异常只能出现在不安全的托管代码中,或在可验证托管代码与非托管代码交互时出现:An AccessViolationException exception can occur only in unsafe managed code or when verifiable managed code interacts with unmanaged code:

在任一情况下,都可以识别并更正异常的原因,如下所示 AccessViolationExceptionIn 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.

自动内存管理是公共语言运行时提供的服务之一。Automatic memory management is one of the services that the common language runtime provides. 如果托管代码与非托管代码提供相同的功能,你可能希望转到托管代码以利用此功能。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 开始, AccessViolationException 如果异常发生在公共语言运行时所保留的内存之外,则由公共语言运行时引发的异常不会由 catch 结构化异常处理程序中的语句处理。Starting with .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, apply the HandleProcessCorruptedStateExceptionsAttribute attribute to the method in which the exception is thrown. 此更改不会影响 AccessViolationException 用户代码引发的异常,但可以继续通过语句来捕获异常 catchThis 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 以前版本编写的代码,可以将该 <legacyCorruptedStateExceptionsPolicy> 元素添加到应用的配置文件中。For code written for previous versions of .NET Framework that you want to recompile and run without modification on .NET Framework 4, you can add the <legacyCorruptedStateExceptionsPolicy> element to your app's configuration file. 请注意,如果已为或事件定义处理程序,则还可以接收例外通知 AppDomain.FirstChanceException AppDomain.UnhandledExceptionNote 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 on .NET Framework 2.0 and later versions. 在早期版本的 .NET Framework 中,非托管代码或不安全的托管代码中的访问冲突由 NullReferenceException 托管代码中的表示。In earlier versions of .NET Framework, an access violation in unmanaged code or unsafe managed code is represented by a NullReferenceException in managed code. NullReferenceException当在可验证托管代码中取消引用空引用、不涉及数据损坏的情况,并且没有办法区分版本1.0 或1.1 中的两种情况时,也会引发。A 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 .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)

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。When 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)