AccessViolationException AccessViolationException AccessViolationException AccessViolationException Class

定义

在试图读写受保护内存时引发的异常。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
继承
AccessViolationExceptionAccessViolationExceptionAccessViolationExceptionAccessViolationException
属性

注解

当代码尝试读取或写入到尚未分配的或不能访问的内存,则将发生访问冲突在非托管或不安全代码。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.

在程序中完全由可验证的托管代码组成,所有引用都是有效或者为空,并都会产生访问冲突。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

从开始.NET Framework 4.NET Framework 4AccessViolationException由公共语言运行时引发的异常未由catch如果异常发生在由公共语言保留的内存外的结构化的异常处理程序中的语句运行时。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 的早期版本编写的代码.NET Framework 4.NET Framework 4,可以添加 <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时为 null 引用取消引用在托管代码中可验证,不涉及数据损坏,匹配项,并没有方法来区分版本 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() AccessViolationException() AccessViolationException()

使用由系统提供的用于描述错误的消息初始化 AccessViolationException 类的新实例。Initializes a new instance of the AccessViolationException class with a system-supplied message that describes the error.

AccessViolationException(SerializationInfo, StreamingContext) AccessViolationException(SerializationInfo, StreamingContext) AccessViolationException(SerializationInfo, StreamingContext) AccessViolationException(SerializationInfo, StreamingContext)

用序列化数据初始化 AccessViolationException 类的新实例。Initializes a new instance of the AccessViolationException class with serialized data.

AccessViolationException(String) AccessViolationException(String) AccessViolationException(String) AccessViolationException(String)

使用指定的描述错误的消息初始化 AccessViolationException 类的新实例。Initializes a new instance of the AccessViolationException class with a specified message that describes the error.

AccessViolationException(String, Exception) AccessViolationException(String, Exception) AccessViolationException(String, Exception) 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 Data Data Data

获取提供有关异常的其他用户定义信息的键/值对集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
HelpLink HelpLink HelpLink HelpLink

获取或设置指向与此异常关联的帮助文件链接。Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult HResult HResult HResult

获取或设置 HRESULT(一个分配给特定异常的编码数字值)。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException InnerException InnerException InnerException

获取导致当前异常的 Exception 实例。Gets the Exception instance that caused the current exception.

(Inherited from Exception)
Message Message Message Message

获取描述当前异常的消息。Gets a message that describes the current exception.

(Inherited from Exception)
Source Source Source Source

获取或设置导致错误的应用程序或对象的名称。Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception)
StackTrace StackTrace StackTrace StackTrace

获取调用堆栈上的即时框架字符串表示形式。Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite TargetSite TargetSite TargetSite

获取引发当前异常的方法。Gets the method that throws the current exception.

(Inherited from Exception)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException() GetBaseException() GetBaseException() GetBaseException()

当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根源。When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

当在派生类中重写时,用关于异常的信息设置 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(Inherited from Exception)
GetType() GetType() GetType() GetType()

获取当前实例的运行时类型。Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

创建并返回当前异常的字符串表示形式。Creates and returns a string representation of the current exception.

(Inherited from Exception)

事件

SerializeObjectState SerializeObjectState SerializeObjectState SerializeObjectState

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

(Inherited from Exception)

适用于

另请参阅