NullReferenceException 类

定义

尝试取消引用空对象引用时引发的异常。The exception that is thrown when there is an attempt to dereference a null object reference.

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

注解

尝试访问值为null的类型的成员时, 将引发异常。NullReferenceExceptionA NullReferenceException exception is thrown when you try to access a member on a type whose value is null. NullReferenceException异常通常反映开发人员错误, 在以下情况下引发:A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios:

  • 您忘记了实例化引用类型。You've forgotten to instantiate a reference type. 在下面的示例中names , 已声明但从未实例化:In the following example, names is declared but never instantiated:

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main(string[] args)
       {
          int value = Int32.Parse(args[0]);
          List<String> names;
          if (value > 0)
             names = new List<String>();
          
          names.Add("Major Major Major");       
       }
    }
    // Compilation displays a warning like the following:
    //    Example1.cs(10) : warning BC42104: Variable //names// is used before it 
    //    has been assigned a value. A null reference exception could result 
    //    at runtime.
    //    
    //          names.Add("Major Major Major")
    //          ~~~~~
    // The example displays output like the following output:
    //    Unhandled Exception: System.NullReferenceException: Object reference 
    //    not set to an instance of an object.
    //       at Example.Main()
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim names As List(Of String)
          names.Add("Major Major Major")       
       End Sub
    End Module
    ' Compilation displays a warning like the following:
    '    Example1.vb(10) : warning BC42104: Variable 'names' is used before it 
    '    has been assigned a value. A null reference exception could result 
    '    at runtime.
    '    
    '          names.Add("Major Major Major")
    '          ~~~~~
    ' The example displays output like the following output:
    '    Unhandled Exception: System.NullReferenceException: Object reference 
    '    not set to an instance of an object.
    '       at Example.Main()
    

    在编译此代码时, 某些编译器会发出警告。Some compilers issue a warning when they compile this code. 其他人发出错误, 编译失败。Others issue an error, and the compilation fails. 若要解决此问题, 请实例化对象, 使其值不再null存在。To address this problem, instantiate the object so that its value is no longer null. 下面的示例通过调用类型的类构造函数来实现此功能。The following example does this by calling a type's class constructor.

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          List<String> names = new List<String>();
          names.Add("Major Major Major");
       }
    }
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim names As New List(Of String)()
          names.Add("Major Major Major")       
       End Sub
    End Module
    
  • 在初始化数组之前, 您已经忘记了维度。You've forgotten to dimension an array before initializing it. 在下面的示例中values , 将声明为整数数组, 但从不指定它包含的元素的数目。In the following example, values is declared to be an integer array, but the number of elements that it contains is never specified. 尝试初始化其值, 因此引发了NullReferenceException异常。The attempt to initialize its values therefore thrown a NullReferenceException exception.

    using System;
    
    public class Example
    {
       public static void Main()
       {
           int[] values = null;
           for (int ctr = 0; ctr <= 9; ctr++)
              values[ctr] = ctr * 2;
              
           foreach (var value in values)
              Console.WriteLine(value);   
       }
    }
    // The example displays the following output:
    //    Unhandled Exception: 
    //       System.NullReferenceException: Object reference not set to an instance of an object.
    //       at Example.Main()
    
    Module Example
       Public Sub Main()
           Dim values() As Integer
           For ctr As Integer = 0 To 9
              values(ctr) = ctr * 2
           Next
              
           For Each value In values
              Console.WriteLine(value)
           Next      
       End Sub
    End Module
    ' The example displays the following output:
    '    Unhandled Exception: 
    '       System.NullReferenceException: Object reference not set to an instance of an object.
    '       at Example.Main()
    

    可以通过在初始化数组中的元素数量之前对其进行声明, 如以下示例中所示。You can eliminate the exception by declaring the number of elements in the array before initializing it, as the following example does.

    using System;
    
    public class Example
    {
       public static void Main()
       {
           int[] values = new int[10];
           for (int ctr = 0; ctr <= 9; ctr++)
              values[ctr] = ctr * 2;
              
           foreach (var value in values)
              Console.WriteLine(value);   
       }
    }
    // The example displays the following output:
    //    0
    //    2
    //    4
    //    6
    //    8
    //    10
    //    12
    //    14
    //    16
    //    18
    
    Module Example
       Public Sub Main()
           Dim values(9) As Integer
           For ctr As Integer = 0 To 9
              values(ctr) = ctr * 2
           Next
              
           For Each value In values
              Console.WriteLine(value)
           Next      
       End Sub
    End Module
    ' The example displays the following output:
    '    0
    '    2
    '    4
    '    6
    '    8
    '    10
    '    12
    '    14
    '    16
    '    18
    

    有关声明和初始化数组的详细信息, 请参阅数组数组For more information on declaring and initializing arrays, see Arrays and Arrays.

  • 从方法获取null返回值, 然后对返回的类型调用方法。You get a null return value from a method, and then call a method on the returned type. 这有时是文档错误的结果;该文档无法注意到, 方法调用可以返回nullThis sometimes is the result of a documentation error; the documentation fails to note that a method call can return null. 在其他情况下, 代码错误地假定方法将始终返回非null值。In other cases, your code erroneously assumes that the method will always return a non-null value.

    以下示例中的代码假定Array.Find方法始终返回PersonFirstName字段与搜索字符串匹配的对象。The code in the following example assumes that the Array.Find method always returns Person object whose FirstName field matches a search string. 由于没有匹配项, 运行时将引发NullReferenceException异常。Because there is no match, the runtime throws a NullReferenceException exception.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Person[] persons = Person.AddRange( new String[] { "Abigail", "Abra", 
                                              "Abraham", "Adrian", "Ariella", 
                                              "Arnold", "Aston", "Astor" } );    
          String nameToFind = "Robert";
          Person found = Array.Find(persons, p => p.FirstName == nameToFind);
          Console.WriteLine(found.FirstName);
       }
    }
    
    public class Person
    {
       public static Person[] AddRange(String[] firstNames) 
       {
          Person[] p = new Person[firstNames.Length];
          for (int ctr = 0; ctr < firstNames.Length; ctr++)
             p[ctr] = new Person(firstNames[ctr]);
    
          return p;
       }
       
       public Person(String firstName)
       {
          this.FirstName = firstName;
       } 
       
       public String FirstName;
    }
    // The example displays the following output:
    //       Unhandled Exception: System.NullReferenceException: 
    //       Object reference not set to an instance of an object.
    //          at Example.Main()
    
    Module Example
       Public Sub Main()
          Dim persons() As Person = Person.AddRange( { "Abigail", "Abra",
                                                       "Abraham", "Adrian",
                                                       "Ariella", "Arnold", 
                                                       "Aston", "Astor" } )    
          Dim nameToFind As String = "Robert"
          Dim found As Person = Array.Find(persons, Function(p) p.FirstName = nameToFind)
          Console.WriteLine(found.FirstName)
       End Sub
    End Module
    
    Public Class Person
       Public Shared Function AddRange(firstNames() As String) As Person()
          Dim p(firstNames.Length - 1) As Person
          For ctr As Integer = 0 To firstNames.Length - 1
             p(ctr) = New Person(firstNames(ctr))
          Next   
          Return p
       End Function
       
       Public Sub New(firstName As String)
          Me.FirstName = firstName
       End Sub 
       
       Public FirstName As String
    End Class
    ' The example displays the following output:
    '       Unhandled Exception: System.NullReferenceException: 
    '       Object reference not set to an instance of an object.
    '          at Example.Main()
    

    若要解决此问题, 请测试方法的返回值, 以确保它不null在调用其任何成员之前, 如以下示例中所示。To address this problem, test the method's return value to ensure that it is not null before calling any of its members, as the following example does.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Person[] persons = Person.AddRange( new String[] { "Abigail", "Abra", 
                                              "Abraham", "Adrian", "Ariella", 
                                              "Arnold", "Aston", "Astor" } );    
          String nameToFind = "Robert";
          Person found = Array.Find(persons, p => p.FirstName == nameToFind);
          if (found != null)
             Console.WriteLine(found.FirstName);
          else
             Console.WriteLine("{0} not found.", nameToFind);   
       }
    }
    
    public class Person
    {
       public static Person[] AddRange(String[] firstNames) 
       {
          Person[] p = new Person[firstNames.Length];
          for (int ctr = 0; ctr < firstNames.Length; ctr++)
             p[ctr] = new Person(firstNames[ctr]);
    
          return p;
       }
       
       public Person(String firstName)
       {
          this.FirstName = firstName;
       } 
       
       public String FirstName;
    }
    // The example displays the following output:
    //        Robert not found
    
    Module Example
       Public Sub Main()
          Dim persons() As Person = Person.AddRange( { "Abigail", "Abra",
                                                       "Abraham", "Adrian",
                                                       "Ariella", "Arnold", 
                                                       "Aston", "Astor" } )    
          Dim nameToFind As String = "Robert"
          Dim found As Person = Array.Find(persons, Function(p) p.FirstName = nameToFind)
          If found IsNot Nothing Then
             Console.WriteLine(found.FirstName)
          Else
             Console.WriteLine("{0} not found.", nameToFind)
          End If   
       End Sub
    End Module
    
    Public Class Person
       Public Shared Function AddRange(firstNames() As String) As Person()
          Dim p(firstNames.Length - 1) As Person
          For ctr As Integer = 0 To firstNames.Length - 1
             p(ctr) = New Person(firstNames(ctr))
          Next   
          Return p
       End Function
       
       Public Sub New(firstName As String)
          Me.FirstName = firstName
       End Sub 
       
       Public FirstName As String
    End Class
    ' The example displays the following output:
    '       Robert not found
    
  • 你使用的是表达式 (例如, 你要将方法或属性的列表链接在一起) 以检索值, 但你要检查值是否为, 则运行null时仍会NullReferenceException引发异常。You're using an expression (for example, you're chaining a list of methods or properties together) to retrieve a value and, although you're checking whether the value is null, the runtime still throws a NullReferenceException exception. 出现这种情况的原因是表达式中的一个中间null值返回了。This occurs because one of the intermediate values in the expression returns null. 因此, 永远不会计算的null测试结果。As a result, your test for null is never evaluated.

    下面的示例定义了Pages一个对象, 该对象缓存有关网页的信息, 这些Page信息由对象提供。The following example defines a Pages object that caches information about web pages, which are presented by Page objects. Example.Main方法检查当前网页是否具有非 null 标题, 如果存在, 则显示标题。The Example.Main method checks whether the current web page has a non-null title and, if it does, displays the title. 尽管进行了此检查, 但该方法会NullReferenceException引发异常。Despite this check, however, the method throws a NullReferenceException exception.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          var pages = new Pages();
          if (! String.IsNullOrEmpty(pages.CurrentPage.Title)) {
             String title = pages.CurrentPage.Title;
             Console.WriteLine("Current title: '{0}'", title);
          }
       }
    }
    
    public class Pages 
    {
       Page[] page = new Page[10];
       int ctr = 0;
       
       public Page CurrentPage
       {
          get { return page[ctr]; }
          set {
             // Move all the page objects down to accommodate the new one.
             if (ctr > page.GetUpperBound(0)) {
                for (int ndx = 1; ndx <= page.GetUpperBound(0); ndx++)
                   page[ndx - 1] = page[ndx];
             }    
             page[ctr] = value;
             if (ctr < page.GetUpperBound(0))
                ctr++; 
          }
       }
       
       public Page PreviousPage
       {
          get {
             if (ctr == 0) { 
                if (page[0] == null)
                   return null;
                else
                   return page[0];
             }
             else {
                ctr--;
                return page[ctr + 1];
             }
          }
       }         
    }
    
    public class Page
    {
       public Uri URL;
       public String Title;
    }
    // The example displays the following output:
    //    Unhandled Exception: 
    //       System.NullReferenceException: Object reference not set to an instance of an object.
    //       at Example.Main()
    
    Module Example
       Public Sub Main()
          Dim pages As New Pages()
          Dim title As String = pages.CurrentPage.Title
       End Sub
    End Module
    
    Public Class Pages 
       Dim page(9) As Page
       Dim ctr As Integer = 0
       
       Public Property CurrentPage As Page
          Get
             Return page(ctr)
          End Get
          Set
             ' Move all the page objects down to accommodate the new one.
             If ctr > page.GetUpperBound(0) Then
                For ndx As Integer = 1 To page.GetUpperBound(0)
                   page(ndx - 1) = page(ndx)
                Next
             End If    
             page(ctr) = value
             If ctr < page.GetUpperBound(0) Then ctr += 1 
          End Set
       End Property
       
       Public ReadOnly Property PreviousPage As Page
          Get
             If ctr = 0 Then 
                If page(0) Is Nothing Then
                   Return Nothing
                Else
                   Return page(0)
                End If   
             Else
                ctr -= 1
                Return page(ctr + 1)
             End If
          End Get
       End Property         
    End Class
    
    Public Class Page
       Public URL As Uri
       Public Title As String
    End Class
    ' The example displays the following output:
    '    Unhandled Exception: 
    '       System.NullReferenceException: Object reference not set to an instance of an object.
    '       at Example.Main()
    

    引发此异常的原因pages.CurrentPagenull , 如果缓存中没有任何页信息, 则返回。The exception is thrown because pages.CurrentPage returns null if no page information is stored in the cache. 在检索当前CurrentPage Page对象的Title属性之前, 可以通过测试属性的值来更正此异常, 如以下示例所示:This exception can be corrected by testing the value of the CurrentPage property before retrieving the current Page object's Title property, as the following example does:

    using System;
    
    public class Example
    {
       public static void Main()
       {
          var pages = new Pages();
          Page current = pages.CurrentPage;
          if (current != null) {  
             String title = current.Title;
             Console.WriteLine("Current title: '{0}'", title);
          }
          else {
             Console.WriteLine("There is no page information in the cache.");
          }   
       }
    }
    // The example displays the following output:
    //       There is no page information in the cache.
    
    Module Example
       Public Sub Main()
          Dim pages As New Pages()
          Dim current As Page = pages.CurrentPage
          If current IsNot Nothing Then 
             Dim title As String = current.Title
             Console.WriteLine("Current title: '{0}'", title)
          Else
             Console.WriteLine("There is no page information in the cache.")
          End If   
       End Sub
    End Module
    ' The example displays the following output:
    '       There is no page information in the cache.
    
  • 你正在枚举包含引用类型的数组元素, 而你尝试处理其中一个元素会引发NullReferenceException异常。You're enumerating the elements of an array that contains reference types, and your attempt to process one of the elements throws a NullReferenceException exception.

    下面的示例定义一个字符串数组。The following example defines a string array. 语句枚举数组中的元素, 并在显示字符串之前调用Trim每个字符串的方法。 forA for statement enumerates the elements in the array and calls each string's Trim method before displaying the string.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          String[] values = { "one", null, "two" };
          for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
             Console.Write("{0}{1}", values[ctr].Trim(), 
                           ctr == values.GetUpperBound(0) ? "" : ", "); 
          Console.WriteLine();
       }
    }
    // The example displays the following output:
    //    Unhandled Exception: 
    //       System.NullReferenceException: Object reference not set to an instance of an object.
    //       at Example.Main()
    
    Module Example
       Public Sub Main()
          Dim values() As String = { "one", Nothing, "two" }
          For ctr As Integer = 0 To values.GetUpperBound(0)
             Console.Write("{0}{1}", values(ctr).Trim(), 
                           If(ctr = values.GetUpperBound(0), "", ", ")) 
          Next
          Console.WriteLine()
       End Sub
    End Module
    ' The example displays the following output:
    '    Unhandled Exception: System.NullReferenceException: 
    '       Object reference not set to an instance of an object.
    '       at Example.Main()
    

    如果假定数组的每个元素都必须包含非 null 值, 并且数组元素的值实际上null为, 则会发生此异常。This exception occurs if you assume that each element of the array must contain a non-null value, and the value of the array element is in fact null. 可以通过测试元素null是否在对该元素执行任何操作之前消除此异常, 如下面的示例所示。The exception can be eliminated by testing whether the element is null before performing any operation on that element, as the following example shows.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          String[] values = { "one", null, "two" };
          for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
             Console.Write("{0}{1}", 
                           values[ctr] != null ? values[ctr].Trim() : "", 
                           ctr == values.GetUpperBound(0) ? "" : ", "); 
          Console.WriteLine();
       }
    }
    // The example displays the following output:
    //       one, , two
    
    Module Example
       Public Sub Main()
          Dim values() As String = { "one", Nothing, "two" }
          For ctr As Integer = 0 To values.GetUpperBound(0)
             Console.Write("{0}{1}", 
                           If(values(ctr) IsNot Nothing, values(ctr).Trim(), ""), 
                           If(ctr = values.GetUpperBound(0), "", ", ")) 
          Next
          Console.WriteLine()
       End Sub
    End Module
    ' The example displays the following output:
    '       one, , two
    
  • NullReferenceException 传递null的方法引发异常。A NullReferenceException exception is thrown by a method that is passed null. 某些方法会验证传递给它们的参数。Some methods validate the arguments that are passed to them. 如果这些参数为null, 则该方法将System.ArgumentNullException引发异常。If they do and one of the arguments is null, the method throws an System.ArgumentNullException exception. 否则, 会引发NullReferenceException异常。Otherwise, it throws a NullReferenceException exception. 下面的示例演示了这种情况。The following example illustrates this scenario.

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          List<String> names = GetData();
          PopulateNames(names);
       }
    
       private static void PopulateNames(List<String> names)
       {
          String[] arrNames = { "Dakota", "Samuel", "Nikita",
                                "Koani", "Saya", "Yiska", "Yumaevsky" };
          foreach (var arrName in arrNames)
             names.Add(arrName);
       }
       
       private static List<String> GetData() 
       {
          return null;   
       
       }
    }
    // The example displays output like the following:
    //    Unhandled Exception: System.NullReferenceException: Object reference 
    //    not set to an instance of an object.
    //       at Example.PopulateNames(List`1 names)
    //       at Example.Main()
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim names As List(Of String) = GetData()
          PopulateNames(names)
       End Sub
       
       Private Sub PopulateNames(names As List(Of String))
          Dim arrNames() As String = { "Dakota", "Samuel", "Nikita",
                                       "Koani", "Saya", "Yiska", "Yumaevsky" }
          For Each arrName In arrNames
             names.Add(arrName)
          Next
       End Sub
       
       Private Function GetData() As List(Of String)
          Return Nothing   
       End Function
    End Module
    ' The example displays output like the following:
    '    Unhandled Exception: System.NullReferenceException: Object reference 
    '    not set to an instance of an object.
    '       at Example.PopulateNames(List`1 names)
    '       at Example.Main()
    

    若要解决此问题, 请确保传递给方法的参数不null是, 或者处理try…catch…finally块中引发的异常。To address this issue, make sure that the argument passed to the method is not null, or handle the thrown exception in a try…catch…finally block. 有关更多信息,请参见 异常中定义的接口的私有 C++ 特定实现。For more information, see Exceptions.

以下 Microsoft 中间NullReferenceException语言 (MSIL) 指令引发: initblk callvirtcpblkcpobj、、 ldelem.<type>ldelema ldfld 、、ldfldaldind.<type>ldlenstelem.<type>、 、stfld 、和unboxstind.<type> throwThe following Microsoft intermediate language (MSIL) instructions throw NullReferenceException: callvirt, cpblk, cpobj, initblk, ldelem.<type>, ldelema, ldfld, ldflda, ldind.<type>, ldlen, stelem.<type>, stfld, stind.<type>, throw, and unbox.

NullReferenceException使用 COR_E_NULLREFERENCE 值为0x80004003 的 HRESULT。NullReferenceException uses the HRESULT COR_E_NULLREFERENCE, which has the value 0x80004003.

有关实例的初始属性值的列表NullReferenceException,请参阅NullReferenceException构造函数。For a list of initial property values for an instance of NullReferenceException, see the NullReferenceException constructors.

处理发行代码中的 NullReferenceExceptionHandling NullReferenceException in release code

通常, 更好的做法是避免 NullReferenceException 在发生后对其进行处理。It's usually better to avoid a NullReferenceException than to handle it after it occurs. 处理异常会导致代码更难维护和理解,并且有时会引入其他 bug。Handling an exception can make your code harder to maintain and understand, and can sometimes introduce other bugs. NullReferenceException 通常为不可恢复的错误。A NullReferenceException is often a non-recoverable error. 在这些情况下,让异常停止应用可能是最佳替代方法。In these cases, letting the exception stop the app might be the best alternative.

然而,在许多情况下,处理错误可能非常有用:However, there are many situations where handling the error can be useful:

  • 你的应用可以忽略为 null 的对象。Your app can ignore objects that are null. 例如,如果你的应用检索并处理数据库中的记录,则你可以忽略一些导致 null 对象的错误记录。For example, if your app retrieves and processes records in a database, you might be able to ignore some number of bad records that result in null objects. 你可能仅需在日志文件或应用程序 UI 中记录错误数据。Recording the bad data in a log file or in the application UI might be all you have to do.

  • 可以从异常中恢复。You can recover from the exception. 例如,在连接丢失或连接超时的情况下,对返回引用类型的 Web 服务的调用可能会返回 null。可尝试重新建立连接并再次尝试调用。For example, a call to a web service that returns a reference type might return null if the connection is lost or the connection times out. You can attempt to reestablish the connection and try the call again.

  • 可将你的应用状态还原到有效状态。You can restore the state of your app to a valid state. 例如,你可能要执行多步骤的任务,该任务需要你在调用引发 NullReferenceException 的方法之前将信息保存到数据存储。For example, you might be performing a multi-step task that requires you to save information to a data store before you call a method that throws a NullReferenceException. 如果未初始化的对象可能会损坏数据记录,你可以在关闭应用之前删除之前的数据。If the uninitialized object would corrupt the data record, you can remove the previous data before you close the app.

  • 你需要报告异常。You want to report the exception. 例如,如果该错误由应用用户的某个错误引起,你可以生成一条可帮助该用户提供正确信息的消息。For example, if the error was caused by a mistake from the user of your app, you can generate a message to help him supply the correct information. 你还可以记录有关该错误的信息来帮助你解决此问题。You can also log information about the error to help you fix the problem. 某些框架(如 ASP.NET)具有高级别的异常处理程序,该处理程序可捕获所有错误,以便应用永远不会发生崩溃,在这种情况下,记录异常可能是你用来了解是否发生异常的唯一方式。Some frameworks, like ASP.NET, have a high-level exception handler that captures all errors to that the app never crashes; in that case, logging the exception might be the only way you can know that it occurs.

构造函数

NullReferenceException()

初始化 NullReferenceException 类的新实例,将新实例的 Message 属性设置为系统提供的描述错误的消息,如“在需要对象实例的地方找到了值‘null’。”。Initializes a new instance of the NullReferenceException class, setting the Message property of the new instance to a system-supplied message that describes the error, such as "The value 'null' was found where an instance of an object was required." 此消息将当前系统区域性考虑在内。This message takes into account the current system culture.

NullReferenceException(SerializationInfo, StreamingContext)

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

NullReferenceException(String)

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

NullReferenceException(String, Exception)

使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 NullReferenceException 类的新实例。Initializes a new instance of the NullReferenceException 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)

适用于

另请参阅