AccessViolationException 类

定义

在试图读写受保护内存时引发的异常。The exception that is thrown when there is an attempt to read or write protected memory.

public ref class AccessViolationException : SystemException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class AccessViolationException : SystemException
type AccessViolationException = class
    inherit SystemException
Public Class AccessViolationException
Inherits SystemException
继承
AccessViolationException
属性

注解

当非托管代码或不安全代码尝试对尚未分配的或不具有访问权限的内存进行读写操作时, 会发生访问冲突。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:

在任一情况下, 都可以识别并更正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.

自动内存管理是公共语言运行时提供的服务之一。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

从开始AccessViolationException catch , 如果异常发生在公共语言保留的内存之外, 则由公共语言运行时引发的异常不会由结构化异常处理程序中的语句处理。 .NET Framework 4.NET Framework 4时会.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 中, 非托管代码或不安全的托管代码中的访问冲突由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. 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 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()

使用由系统提供的用于描述错误的消息初始化 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(String)

使用指定的描述错误的消息初始化 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.

属性

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 a message that describes the current exception.

(继承自 Exception)
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

获取引发当前异常的方法。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)

当在派生类中重写时,用关于异常的信息设置 SerializationInfoWhen 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

当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(继承自 Exception)

适用于

另请参阅