public ref class Exception
public ref class Exception : System::Runtime::Serialization::ISerializable
public ref class Exception : System::Runtime::InteropServices::_Exception, System::Runtime::Serialization::ISerializable
public class Exception
public class Exception : System.Runtime.Serialization.ISerializable
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)]
[System.Serializable]
public class Exception : System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public class Exception : System.Runtime.InteropServices._Exception, System.Runtime.Serialization.ISerializable
[System.Serializable]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public class Exception : System.Runtime.Serialization.ISerializable
type Exception = class
type Exception = class
    interface ISerializable
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.AutoDual)>]
[<System.Serializable>]
type Exception = class
    interface ISerializable
[<System.Serializable>]
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Exception = class
    interface ISerializable
    interface _Exception
[<System.Serializable>]
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Exception = class
    interface ISerializable
Public Class Exception
Public Class Exception
Implements ISerializable
Public Class Exception
Implements _Exception, ISerializable
Object
Exception

以下示例演示 catch 了 F#) 块中定义的用于处理 ArithmeticException 错误的 ( with 。 此 catch 块还会捕获 DivideByZeroException 错误,因为 DivideByZeroException 派生自 ArithmeticException 且没有 catch 针对错误显式定义的 DivideByZeroException 块。

using namespace System; int main() int x = 0; int y = 100 / x; catch ( ArithmeticException^ e ) Console::WriteLine( "ArithmeticException Handler: {0}", e ); catch ( Exception^ e ) Console::WriteLine( "Generic Exception Handler: {0}", e ); This code example produces the following results: ArithmeticException Handler: System.DivideByZeroException: Attempted to divide by zero. at main() using System; class ExceptionTestClass public static void Main() int x = 0; int y = 100 / x; catch (ArithmeticException e) Console.WriteLine($"ArithmeticException Handler: {e}"); catch (Exception e) Console.WriteLine($"Generic Exception Handler: {e}"); This code example produces the following results: ArithmeticException Handler: System.DivideByZeroException: Attempted to divide by zero. at ExceptionTestClass.Main() module ExceptionTestModule open System let x = 0 let y = 100 / x | :? ArithmeticException as e -> printfn $"ArithmeticException Handler: {e}" | e -> printfn $"Generic Exception Handler: {e}" // This code example produces the following results: // ArithmeticException Handler: System.DivideByZeroException: Attempted to divide by zero. // at <StartupCode$fs>.$ExceptionTestModule.main@() Class ExceptionTestClass Public Shared Sub Main() Dim x As Integer = 0 Dim y As Integer = 100 / x Catch e As ArithmeticException Console.WriteLine("ArithmeticException Handler: {0}", e.ToString()) Catch e As Exception Console.WriteLine("Generic Exception Handler: {0}", e.ToString()) End Try End Sub End Class 'This code example produces the following results: 'ArithmeticException Handler: System.OverflowException: Arithmetic operation resulted in an overflow. ' at ExceptionTestClass.Main()

此类是所有异常的基类。 发生错误时,系统或当前正在执行的应用程序会引发包含有关错误的信息的异常来报告它。 引发异常后,应用程序或默认异常处理程序处理该异常。

本部分内容:

错误和异常
Try/catch 块
异常类型功能
异常类属性
性能注意事项
重新引发异常
选择标准异常
实现自定义异常

错误和异常

由于各种原因,可能会发生运行时错误。 但是,并非所有错误都应作为代码中的异常进行处理。 下面是一些可在运行时发生的错误类别,以及响应这些错误的适当方法。

  • 使用错误。 使用错误表示程序逻辑中可能导致异常的错误。 但是,错误应不通过异常处理来解决,而应通过修改错误代码来解决。 例如,以下示例中方法的 Object.Equals(Object) 重写假定 obj 参数必须始终为非 null。

    using System; public class Person private string _name; public string Name get { return _name; } set { _name = value; } public override int GetHashCode() return this.Name.GetHashCode(); public override bool Equals(object obj) // This implementation contains an error in program logic: // It assumes that the obj argument is not null. Person p = (Person) obj; return this.Name.Equals(p.Name); public class Example public static void Main() Person p1 = new Person(); p1.Name = "John"; Person p2 = null; // The following throws a NullReferenceException. Console.WriteLine("p1 = p2: {0}", p1.Equals(p2)); // In F#, null is not a valid state for declared types // without 'AllowNullLiteralAttribute' [<AllowNullLiteral>] type Person() = member val Name = "" with get, set override this.GetHashCode() = this.Name.GetHashCode() override this.Equals(obj) = // This implementation contains an error in program logic: // It assumes that the obj argument is not null. let p = obj :?> Person this.Name.Equals p.Name let p1 = Person() p1.Name <- "John" let p2: Person = null // The following throws a NullReferenceException. printfn $"p1 = p2: {p1.Equals p2}" Public Class Person Private _name As String Public Property Name As String Return _name End Get _name = value End Set End Property Public Overrides Function Equals(obj As Object) As Boolean ' This implementation contains an error in program logic: ' It assumes that the obj argument is not null. Dim p As Person = CType(obj, Person) Return Me.Name.Equals(p.Name) End Function End Class Module Example Public Sub Main() Dim p1 As New Person() p1.Name = "John" Dim p2 As Person = Nothing ' The following throws a NullReferenceException. Console.WriteLine("p1 = p2: {0}", p1.Equals(p2)) End Sub End Module

    NullReferenceException 通过修改源代码以在调用 Object.Equals 重写并重新编译之前显式测试 null,从而消除导致何时 obj``null 消除的异常。 以下示例包含处理 null 参数的更正源代码。

    using System; public class Person private string _name; public string Name get { return _name; } set { _name = value; } public override int GetHashCode() return this.Name.GetHashCode(); public override bool Equals(object obj) // This implementation handles a null obj argument. Person p = obj as Person; if (p == null) return false; return this.Name.Equals(p.Name); public class Example public static void Main() Person p1 = new Person(); p1.Name = "John"; Person p2 = null; Console.WriteLine("p1 = p2: {0}", p1.Equals(p2)); // The example displays the following output: // p1 = p2: False // In F#, null is not a valid state for declared types // without 'AllowNullLiteralAttribute' [<AllowNullLiteral>] type Person() = member val Name = "" with get, set override this.GetHashCode() = this.Name.GetHashCode() override this.Equals(obj) = // This implementation handles a null obj argument. match obj with | :? Person as p -> this.Name.Equals p.Name | _ -> false let p1 = Person() p1.Name <- "John" let p2: Person = null printfn $"p1 = p2: {p1.Equals p2}" // The example displays the following output: // p1 = p2: False Public Class Person Private _name As String Public Property Name As String Return _name End Get _name = value End Set End Property Public Overrides Function Equals(obj As Object) As Boolean ' This implementation handles a null obj argument. Dim p As Person = TryCast(obj, Person) If p Is Nothing Then Return False Return Me.Name.Equals(p.Name) End If End Function End Class Module Example Public Sub Main() Dim p1 As New Person() p1.Name = "John" Dim p2 As Person = Nothing Console.WriteLine("p1 = p2: {0}", p1.Equals(p2)) End Sub End Module ' The example displays the following output: ' p1 = p2: False

    可以使用该方法 Debug.Assert 来识别调试版本中的使用情况错误,以及 Trace.Assert 用于识别调试和发布版本中的使用情况错误的方法,而不是对使用情况错误进行异常处理。 有关详细信息,请参阅 托管代码中的断言

  • 程序错误。 程序错误是一个运行时错误,无法一定通过编写无 bug 代码来避免此错误。

    在某些情况下,程序错误可能反映预期或例程错误条件。 在这种情况下,你可能希望避免使用异常处理来处理程序错误,而是重试该操作。 例如,如果用户预期以特定格式输入日期,则可以通过调用 DateTime.TryParseExact 该方法来分析日期字符串,该方法返回一个 Boolean 值,该值指示分析操作是否成功,而不是使用 DateTime.ParseExact 该方法,如果在无法将日期字符串转换为 DateTime 值时引发 FormatException 异常。 同样,如果用户尝试打开不存在的文件,可以首先调用 File.Exists 该方法来检查文件是否存在,如果不存在,则提示用户是否创建该文件。

    在其他情况下,程序错误反映可以在代码中处理的意外错误条件。 例如,即使已检查以确保文件存在,也可以在打开文件之前将其删除,或者文件可能已损坏。 在这种情况下,尝试通过实例化 StreamReader 对象或调用 Open 方法打开文件可能会引发 FileNotFoundException 异常。 在这些情况下,应使用异常处理从错误中恢复。

  • 系统故障。 系统故障是一个运行时错误,无法以有意义的方式以编程方式处理。 例如,如果公共语言运行时无法分配其他内存,则任何方法都可能会引发 OutOfMemoryException 异常。 通常,系统故障不是使用异常处理来处理的。 相反,可以使用事件(例如 AppDomain.UnhandledException 并调用 Environment.FailFast 该方法来记录异常信息并在应用程序终止前通知用户失败)。

    Try/catch 块

    公共语言运行时提供一个异常处理模型,该模型基于异常作为对象的表示形式,以及程序代码和异常处理代码的分离到 try 块和 catch 块中。 可以有一个或多个 catch 块,每个块旨在处理特定类型的异常,或者一个块旨在捕获比另一个块更具体的异常。

    如果应用程序处理执行应用程序代码块期间发生的异常,则必须将代码放置在语句中 try ,并且称为 try 块。 处理块引发 try 的异常的应用程序代码放置在语句中 catch ,称为 catch 块。 零个或多个 catch 块与块 try 相关联,每个 catch 块都包含一个类型筛选器,用于确定它处理的异常类型。

    在块中 try 发生异常时,系统会按应用程序代码中显示的顺序搜索关联的 catch 块,直到找到 catch 处理异常的块。 如果 catch 块的类型筛选器指定 T 或派生自的任何类型的 T 类型,则 catch 块将处理类型的 T 异常。 系统在找到处理异常的第一个 catch 块后停止搜索。 因此,在应用程序代码中, catch 必须在处理其基类型的块之前 catch 指定处理类型的块,如本部分后面的示例所示。 最后指定句柄 System.Exception 的 catch 块。

    catch 如果没有与当前 try 块关联的块处理异常,并且当前 try 块嵌套在当前调用中的其他 try 块中, catch 则会搜索与下一个封闭 try 块关联的块。 catch 如果未找到异常块,系统将在当前调用中搜索以前的嵌套级别。 如果在当前调用中找不到 catch 异常块,则会将异常传出调用堆栈,并搜索上一个 catch 堆栈帧来处理异常的块。 调用堆栈的搜索将继续执行,直到处理异常,或者调用堆栈上没有更多帧存在。 如果在找不到 catch 处理异常的块的情况下到达调用堆栈的顶部,则默认异常处理程序将处理该异常,应用程序将终止。

    F# try..with Expression

    F# 不使用 catch 块。 相反,引发的异常是使用单个 with 块匹配的模式。 由于这是表达式,而不是语句,因此所有路径都必须返回相同的类型。 若要了解详细信息,请参阅 尝试...带有表达式

    异常类型功能

    异常类型支持以下功能:

  • 描述错误的人工可读文本。 发生异常时,运行时会发出一条短信,告知用户错误的性质,并建议解决问题的操作。 此文本消息保存在异常对象的属性中 Message 。 在创建异常对象期间,可以将文本字符串传递给构造函数来描述该特定异常的详细信息。 如果未向构造函数提供错误消息参数,则使用默认错误消息。 有关更多信息,请参见 Message 属性。

  • 引发异常时调用堆栈的状态。 该 StackTrace 属性承载可用于确定代码中发生错误的堆栈跟踪。 堆栈跟踪列出了调用的源文件中的所有调用方法和行号。

    异常类属性

    Exception 类包含许多属性,这些属性有助于标识代码位置、类型、帮助文件以及异常原因: StackTrace 、、 InnerException 、、 Message HelpLink HResult 、、 Source TargetSite Data

    当两个或多个异常之间存在因果关系时,该 InnerException 属性将维护此信息。 外部异常在响应此内部异常时引发。 处理外部异常的代码可以使用早期内部异常中的信息来更适当地处理错误。 有关异常的补充信息可以存储为属性中的 Data 键/值对的集合。

    在创建异常对象期间传递给构造函数的错误消息字符串应本地化,并且可以使用该类从资源文件 ResourceManager 提供。 有关本地化资源的详细信息,请参阅 “创建附属程序集 打包和部署资源” 主题。

    若要为用户提供有关异常发生原因的详细信息,该 HelpLink 属性可以保留指向帮助文件的 URL (或 URN) 。

    Exception 类使用 HRESULT COR_E_EXCEPTION,该值0x80131500。

    有关类实例 Exception 的初始属性值列表,请参阅 Exception 构造函数。

    性能注意事项

    引发或处理异常会占用大量系统资源和执行时间。 引发异常仅处理真正非凡的条件,而不是处理可预测的事件或流控制。 例如,在某些情况下,例如在开发类库时,如果方法参数无效,则引发异常是合理的,因为预期使用有效参数调用方法。 无效的方法参数(如果不是使用错误的结果)表示发生了异常。 相反,如果用户输入无效,请不要引发异常,因为用户有时可能会输入无效数据。 请改为提供重试机制,以便用户可以输入有效的输入。 也不应使用异常来处理使用错误。 请改用 断言 来识别和更正使用错误。

    此外,当返回代码足够时不要引发异常;不要将返回代码转换为异常;并且不定期捕获异常,请忽略它,然后继续处理。

    重新引发异常

    在许多情况下,异常处理程序只想将异常传递给调用方。 这最常发生在:

  • 类库反过来包装对 .NET Framework类库或其他类库中方法的调用。

  • 遇到致命异常的应用程序或库。 异常处理程序可以记录异常,然后重新引发异常。

    重新引发异常的建议方法是只需在 C# 中使用 throw 语句、F# 中的 重新调用 函数和 Visual Basic 中的 Throw 语句而不包括表达式。 这可确保在将异常传播到调用方时保留所有调用堆栈信息。 下面的示例对此进行了演示。 字符串扩展方法 FindOccurrences 包装对 String.IndexOf(String, Int32) 一个或多个调用,而无需事先验证其参数。

    using System; using System.Collections.Generic; using System.Runtime.CompilerServices; public static class Library public static int[] FindOccurrences(this String s, String f) var indexes = new List<int>(); int currentIndex = 0; try { while (currentIndex >= 0 && currentIndex < s.Length) { currentIndex = s.IndexOf(f, currentIndex); if (currentIndex >= 0) { indexes.Add(currentIndex); currentIndex++; catch (ArgumentNullException e) { // Perform some action here, such as logging this exception. throw; return indexes.ToArray(); open System module Library = let findOccurrences (s: string) (f: string) = let indexes = ResizeArray() let mutable currentIndex = 0 while currentIndex >= 0 && currentIndex < s.Length do currentIndex <- s.IndexOf(f, currentIndex) if currentIndex >= 0 then indexes.Add currentIndex currentIndex <- currentIndex + 1 with :? ArgumentNullException -> // Perform some action here, such as logging this exception. reraise () indexes.ToArray() Imports System.Collections.Generic Imports System.Runtime.CompilerServices Public Module Library <Extension()> Public Function FindOccurrences(s As String, f As String) As Integer() Dim indexes As New List(Of Integer) Dim currentIndex As Integer = 0 Do While currentIndex >= 0 And currentIndex < s.Length currentIndex = s.IndexOf(f, currentIndex) If currentIndex >= 0 Then indexes.Add(currentIndex) currentIndex += 1 End If Catch e As ArgumentNullException ' Perform some action here, such as logging this exception. Throw End Try Return indexes.ToArray() End Function End Module

    调用方随后调用 FindOccurrences 两次。 在对第二次调用 FindOccurrences 中,调用方传递一个 null 作为搜索字符串,这会导致 String.IndexOf(String, Int32) 该方法引发 ArgumentNullException 异常。 此异常由 FindOccurrences 该方法处理,并传回调用方。 由于 throw 语句与没有表达式一起使用,因此该示例的输出显示保留调用堆栈。

    public class Example public static void Main() String s = "It was a cold day when..."; int[] indexes = s.FindOccurrences("a"); ShowOccurrences(s, "a", indexes); Console.WriteLine(); String toFind = null; try { indexes = s.FindOccurrences(toFind); ShowOccurrences(s, toFind, indexes); catch (ArgumentNullException e) { Console.WriteLine("An exception ({0}) occurred.", e.GetType().Name); Console.WriteLine("Message:\n {0}\n", e.Message); Console.WriteLine("Stack Trace:\n {0}\n", e.StackTrace); private static void ShowOccurrences(String s, String toFind, int[] indexes) Console.Write("'{0}' occurs at the following character positions: ", toFind); for (int ctr = 0; ctr < indexes.Length; ctr++) Console.Write("{0}{1}", indexes[ctr], ctr == indexes.Length - 1 ? "" : ", "); Console.WriteLine(); // The example displays the following output: // 'a' occurs at the following character positions: 4, 7, 15 // An exception (ArgumentNullException) occurred. // Message: // Value cannot be null. // Parameter name: value // Stack Trace: // at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri // ngComparison comparisonType) // at Library.FindOccurrences(String s, String f) // at Example.Main() open Library let showOccurrences toFind (indexes: int[]) = printf $"'{toFind}' occurs at the following character positions: " for i = 0 to indexes.Length - 1 do printf $"""{indexes[i]}{if i = indexes.Length - 1 then "" else ", "}""" printfn "" let s = "It was a cold day when..." let indexes = findOccurrences s "a" showOccurrences "a" indexes printfn "" let toFind: string = null let indexes = findOccurrences s toFind showOccurrences toFind indexes with :? ArgumentNullException as e -> printfn $"An exception ({e.GetType().Name}) occurred." printfn $"Message:\n {e.Message}\n" printfn $"Stack Trace:\n {e.StackTrace}\n" // The example displays the following output: // 'a' occurs at the following character positions: 4, 7, 15 // An exception (ArgumentNullException) occurred. // Message: // Value cannot be null. (Parameter 'value') // Stack Trace: // at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri // ngComparison comparisonType) // at Library.findOccurrences(String s, String f) // at <StartupCode$fs>.main@() Module Example Public Sub Main() Dim s As String = "It was a cold day when..." Dim indexes() As Integer = s.FindOccurrences("a") ShowOccurrences(s, "a", indexes) Console.WriteLine() Dim toFind As String = Nothing indexes = s.FindOccurrences(toFind) ShowOccurrences(s, toFind, indexes) Catch e As ArgumentNullException Console.WriteLine("An exception ({0}) occurred.", e.GetType().Name) Console.WriteLine("Message:{0} {1}{0}", vbCrLf, e.Message) Console.WriteLine("Stack Trace:{0} {1}{0}", vbCrLf, e.StackTrace) End Try End Sub Private Sub ShowOccurrences(s As String, toFind As String, indexes As Integer()) Console.Write("'{0}' occurs at the following character positions: ", toFind) For ctr As Integer = 0 To indexes.Length - 1 Console.Write("{0}{1}", indexes(ctr), If(ctr = indexes.Length - 1, "", ", ")) Console.WriteLine() End Sub End Module ' The example displays the following output: ' 'a' occurs at the following character positions: 4, 7, 15 ' An exception (ArgumentNullException) occurred. ' Message: ' Value cannot be null. ' Parameter name: value ' Stack Trace: ' at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri ' ngComparison comparisonType) ' at Library.FindOccurrences(String s, String f) ' at Example.Main()

    相反,如果使用 重新引发异常

    throw e;
    
    Throw e  
    
    raise e
    

    语句,不保留完整调用堆栈,该示例将生成以下输出:

    'a' occurs at the following character positions: 4, 7, 15  
    An exception (ArgumentNullException) occurred.  
    Message:  
       Value cannot be null.  
    Parameter name: value  
    Stack Trace:  
          at Library.FindOccurrences(String s, String f)  
       at Example.Main()  
    

    稍微繁琐的替代方法是引发新异常,并在内部异常中保留原始异常的调用堆栈信息。 然后,调用方可以使用新异常 InnerException 的属性检索堆栈帧和其他有关原始异常的信息。 在这种情况下,throw 语句为:

    throw new ArgumentNullException("You must supply a search string.", raise (ArgumentNullException("You must supply a search string.", e) ) Throw New ArgumentNullException("You must supply a search string.",

    处理异常的用户代码必须知道该 InnerException 属性包含有关原始异常的信息,如以下异常处理程序所示。

    try { indexes = s.FindOccurrences(toFind); ShowOccurrences(s, toFind, indexes); catch (ArgumentNullException e) { Console.WriteLine("An exception ({0}) occurred.", e.GetType().Name); Console.WriteLine(" Message:\n{0}", e.Message); Console.WriteLine(" Stack Trace:\n {0}", e.StackTrace); Exception ie = e.InnerException; if (ie != null) { Console.WriteLine(" The Inner Exception:"); Console.WriteLine(" Exception Name: {0}", ie.GetType().Name); Console.WriteLine(" Message: {0}\n", ie.Message); Console.WriteLine(" Stack Trace:\n {0}\n", ie.StackTrace); // The example displays the following output: // 'a' occurs at the following character positions: 4, 7, 15 // An exception (ArgumentNullException) occurred. // Message: You must supply a search string. // Stack Trace: // at Library.FindOccurrences(String s, String f) // at Example.Main() // The Inner Exception: // Exception Name: ArgumentNullException // Message: Value cannot be null. // Parameter name: value // Stack Trace: // at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri // ngComparison comparisonType) // at Library.FindOccurrences(String s, String f) let indexes = findOccurrences s toFind showOccurrences toFind indexes with :? ArgumentNullException as e -> printfn $"An exception ({e.GetType().Name}) occurred." printfn $" Message:\n{e.Message}" printfn $" Stack Trace:\n {e.StackTrace}" let ie = e.InnerException if ie <> null then printfn " The Inner Exception:" printfn $" Exception Name: {ie.GetType().Name}" printfn $" Message: {ie.Message}\n" printfn $" Stack Trace:\n {ie.StackTrace}\n" // The example displays the following output: // 'a' occurs at the following character positions: 4, 7, 15 // An exception (ArgumentNullException) occurred. // Message: You must supply a search string. // Stack Trace: // at Library.FindOccurrences(String s, String f) // at Example.Main() // The Inner Exception: // Exception Name: ArgumentNullException // Message: Value cannot be null. // Parameter name: value // Stack Trace: // at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri // ngComparison comparisonType) // at Library.FindOccurrences(String s, String f) indexes = s.FindOccurrences(toFind) ShowOccurrences(s, toFind, indexes) Catch e As ArgumentNullException Console.WriteLine("An exception ({0}) occurred.", e.GetType().Name) Console.WriteLine(" Message: {1}{0}", vbCrLf, e.Message) Console.WriteLine(" Stack Trace:{0} {1}{0}", vbCrLf, e.StackTrace) Dim ie As Exception = e.InnerException If ie IsNot Nothing Then Console.WriteLine(" The Inner Exception:") Console.WriteLine(" Exception Name: {0}", ie.GetType().Name) Console.WriteLine(" Message: {1}{0}", vbCrLf, ie.Message) Console.WriteLine(" Stack Trace:{0} {1}{0}", vbCrLf, ie.StackTrace) End If End Try ' The example displays the following output: ' 'a' occurs at the following character positions: 4, 7, 15 ' An exception (ArgumentNullException) occurred. ' Message: You must supply a search string. ' Stack Trace: ' at Library.FindOccurrences(String s, String f) ' at Example.Main() ' The Inner Exception: ' Exception Name: ArgumentNullException ' Message: Value cannot be null. ' Parameter name: value ' Stack Trace: ' at System.String.IndexOf(String value, Int32 startIndex, Int32 count, Stri ' ngComparison comparisonType) ' at Library.FindOccurrences(String s, String f)

    选择标准异常

    必须引发异常时,通常可以在.NET Framework中使用现有异常类型,而不是实现自定义异常。 应在以下两个条件下使用标准异常类型:

  • 将引发由使用错误 ((即调用方法) 的开发人员在程序逻辑中的错误)引发异常。 通常,将引发异常,例如ArgumentExceptionArgumentNullExceptionInvalidOperationExceptionNotSupportedException。 实例化异常对象时提供给异常对象的构造函数的字符串应描述错误,以便开发人员可以修复此错误。 有关更多信息,请参见 Message 属性。

  • 你正在处理一个错误,该错误可以通过现有的.NET Framework异常与调用方通信。 应引发最可能的派生异常。 例如,如果方法要求参数是枚举类型的有效成员,则应引发 InvalidEnumArgumentException (最派生的类) 而不是一个 ArgumentException

    下表列出了常见的异常类型和引发它们的条件。

  • 当异常反映无法映射到现有.NET Framework异常的唯一程序错误时。

  • 当异常要求处理与适用于现有.NET Framework异常的处理不同时,或者异常必须从类似的异常中消除。 例如,如果在分析超出目标整型范围的字符串的数字表示形式时引发 ArgumentOutOfRangeException 异常,则不希望对调用方在调用该方法时未提供相应约束值的错误使用相同的异常。

    Exception类是.NET Framework中所有异常的基类。 许多派生类依赖于类成员的 Exception 继承行为;它们不重写成员,也不定义任何唯一成员 Exception

    定义自己的异常类:

  • 定义继承自 Exception. 的类。 如有必要,请定义类所需的任何唯一成员,以提供有关异常的其他信息。 例如,该 ArgumentException 类包含一个 ParamName 属性,该属性指定参数的名称,其参数导致异常,并且 RegexMatchTimeoutException 该属性包含一个 MatchTimeout 指示超时间隔的属性。

  • 如有必要,请重写要更改或修改其功能的任何继承成员。 请注意,大多数现有的派生类 Exception 不会替代继承成员的行为。

  • 确定自定义异常对象是否可序列化。 序列化使你能够保存有关异常的信息,并允许服务器和客户端代理在远程处理上下文中共享异常信息。 若要使异常对象可序列化,请使用属性对其进行 SerializableAttribute 标记。

  • 定义异常类的构造函数。 通常,异常类具有以下一个或多个构造函数:

  • Exception(),它使用默认值初始化新异常对象的属性。

  • Exception(String),它用指定的错误消息初始化新的异常对象。

  • Exception(String, Exception),它使用指定的错误消息和内部异常初始化新的异常对象。

  • Exception(SerializationInfo, StreamingContext),它是从 protected 序列化数据初始化新异常对象的构造函数。 如果选择使异常对象可序列化,则应实现此构造函数。

    下面的示例演示了自定义异常类的使用。 它定义一个 NotPrimeException 异常,当客户端尝试通过指定不是质数的起始数字来检索一系列质数时引发的异常。 该异常定义一个新属性, NonPrime该属性返回导致异常的非质数。 除了实现受保护的无参数构造函数和具有 SerializationInfo 序列化参数的构造函数外 StreamingContext ,该 NotPrimeException 类还定义了另外三个构造 NonPrime 函数来支持该属性。 除了保留非质数的值外,每个构造函数还调用基类构造函数。 该 NotPrimeException 类还标有 SerializableAttribute 该属性。

    using System; using System.Runtime.Serialization; [Serializable()] public class NotPrimeException : Exception private int notAPrime; protected NotPrimeException() : base() public NotPrimeException(int value) : base(String.Format("{0} is not a prime number.", value)) notAPrime = value; public NotPrimeException(int value, string message) : base(message) notAPrime = value; public NotPrimeException(int value, string message, Exception innerException) : base(message, innerException) notAPrime = value; protected NotPrimeException(SerializationInfo info, StreamingContext context) : base(info, context) public int NonPrime { get { return notAPrime; } } namespace global open System open System.Runtime.Serialization [<Serializable>] type NotPrimeException = inherit Exception val notAPrime: int member this.NonPrime = this.notAPrime new (value) = { inherit Exception($"%i{value} is not a prime number."); notAPrime = value } new (value, message) = { inherit Exception(message); notAPrime = value } new (value, message, innerException: Exception) = { inherit Exception(message, innerException); notAPrime = value } // F# does not support protected members new () = { inherit Exception(); notAPrime = 0 } new (info: SerializationInfo, context: StreamingContext) = { inherit Exception(info, context); notAPrime = 0 } Imports System.Runtime.Serialization <Serializable()> _ Public Class NotPrimeException : Inherits Exception Private notAPrime As Integer Protected Sub New() MyBase.New() End Sub Public Sub New(value As Integer) MyBase.New(String.Format("{0} is not a prime number.", value)) notAPrime = value End Sub Public Sub New(value As Integer, message As String) MyBase.New(message) notAPrime = value End Sub Public Sub New(value As Integer, message As String, innerException As Exception) MyBase.New(message, innerException) notAPrime = value End Sub Protected Sub New(info As SerializationInfo, context As StreamingContext) MyBase.New(info, context) End Sub Public ReadOnly Property NonPrime As Integer Return notAPrime End Get End Property End Class

    PrimeNumberGenerator以下示例中显示的类使用 Eratosthenes 的 Sieve 计算从 2 到客户端在其类构造函数调用中指定的限制的质数序列。 该方法 GetPrimesFrom 返回大于或等于指定下限的所有质数,但如果该下限不是质数,则引发该 NotPrimeException 下限。

    using System; using System.Collections.Generic; [Serializable] public class PrimeNumberGenerator private const int START = 2; private int maxUpperBound = 10000000; private int upperBound; private bool[] primeTable; private List<int> primes = new List<int>(); public PrimeNumberGenerator(int upperBound) if (upperBound > maxUpperBound) string message = String.Format( "{0} exceeds the maximum upper bound of {1}.", upperBound, maxUpperBound); throw new ArgumentOutOfRangeException(message); this.upperBound = upperBound; // Create array and mark 0, 1 as not prime (True). primeTable = new bool[upperBound + 1]; primeTable[0] = true; primeTable[1] = true; // Use Sieve of Eratosthenes to determine prime numbers. for (int ctr = START; ctr <= (int)Math.Ceiling(Math.Sqrt(upperBound)); ctr++) if (primeTable[ctr]) continue; for (int multiplier = ctr; multiplier <= upperBound / ctr; multiplier++) if (ctr * multiplier <= upperBound) primeTable[ctr * multiplier] = true; // Populate array with prime number information. int index = START; while (index != -1) index = Array.FindIndex(primeTable, index, (flag) => !flag); if (index >= 1) primes.Add(index); index++; public int[] GetAllPrimes() return primes.ToArray(); public int[] GetPrimesFrom(int prime) int start = primes.FindIndex((value) => value == prime); if (start < 0) throw new NotPrimeException(prime, String.Format("{0} is not a prime number.", prime)); return primes.FindAll((value) => value >= prime).ToArray(); namespace global open System [<Serializable>] type PrimeNumberGenerator(upperBound) = let start = 2 let maxUpperBound = 10000000 let primes = ResizeArray() let primeTable = upperBound + 1 |> Array.zeroCreate<bool> if upperBound > maxUpperBound then let message = $"{upperBound} exceeds the maximum upper bound of {maxUpperBound}." raise (ArgumentOutOfRangeException message) // Create array and mark 0, 1 as not prime (True). primeTable[0] <- true primeTable[1] <- true // Use Sieve of Eratosthenes to determine prime numbers. for i = start to float upperBound |> sqrt |> ceil |> int do if not primeTable[i] then for multiplier = i to upperBound / i do if i * multiplier <= upperBound then primeTable[i * multiplier] <- true // Populate array with prime number information. let mutable index = start while index <> -1 do index <- Array.FindIndex(primeTable, index, fun flag -> not flag) if index >= 1 then primes.Add index index <- index + 1 member _.GetAllPrimes() = primes.ToArray() member _.GetPrimesFrom(prime) = let start = Seq.findIndex ((=) prime) primes if start < 0 then raise (NotPrimeException(prime, $"{prime} is not a prime number.") ) Seq.filter ((>=) prime) primes |> Seq.toArray Imports System.Collections.Generic <Serializable()> Public Class PrimeNumberGenerator Private Const START As Integer = 2 Private maxUpperBound As Integer = 10000000 Private upperBound As Integer Private primeTable() As Boolean Private primes As New List(Of Integer) Public Sub New(upperBound As Integer) If upperBound > maxUpperBound Then Dim message As String = String.Format( "{0} exceeds the maximum upper bound of {1}.", upperBound, maxUpperBound) Throw New ArgumentOutOfRangeException(message) End If Me.upperBound = upperBound ' Create array and mark 0, 1 as not prime (True). ReDim primeTable(upperBound) primeTable(0) = True primeTable(1) = True ' Use Sieve of Eratosthenes to determine prime numbers. For ctr As Integer = START To CInt(Math.Ceiling(Math.Sqrt(upperBound))) If primeTable(ctr) Then Continue For For multiplier As Integer = ctr To CInt(upperBound \ ctr) If ctr * multiplier <= upperBound Then primeTable(ctr * multiplier) = True ' Populate array with prime number information. Dim index As Integer = START Do While index <> -1 index = Array.FindIndex(primeTable, index, Function(flag) Return Not flag End Function) If index >= 1 Then primes.Add(index) index += 1 End If End Sub Public Function GetAllPrimes() As Integer() Return primes.ToArray() End Function Public Function GetPrimesFrom(prime As Integer) As Integer() Dim start As Integer = primes.FindIndex(Function(value) Return value = prime End Function) If start < 0 Then Throw New NotPrimeException(prime, String.Format("{0} is not a prime number.", prime)) Return primes.FindAll(Function(value) Return value >= prime End Function).ToArray() End If End Function End Class

    以下示例使用非质数对 GetPrimesFrom 方法进行两次调用,其中一个调用跨越应用程序域边界。 在这两种情况下,异常都将在客户端代码中引发并成功处理。

    using System; using System.Reflection; class Example public static void Main() int limit = 10000000; PrimeNumberGenerator primes = new PrimeNumberGenerator(limit); int start = 1000001; int[] values = primes.GetPrimesFrom(start); Console.WriteLine("There are {0} prime numbers from {1} to {2}", start, limit); catch (NotPrimeException e) Console.WriteLine("{0} is not prime", e.NonPrime); Console.WriteLine(e); Console.WriteLine("--------"); AppDomain domain = AppDomain.CreateDomain("Domain2"); PrimeNumberGenerator gen = (PrimeNumberGenerator)domain.CreateInstanceAndUnwrap( typeof(Example).Assembly.FullName, "PrimeNumberGenerator", true, BindingFlags.Default, null, new object[] { 1000000 }, null, null); start = 100; Console.WriteLine(gen.GetPrimesFrom(start)); catch (NotPrimeException e) Console.WriteLine("{0} is not prime", e.NonPrime); Console.WriteLine(e); Console.WriteLine("--------"); open System open System.Reflection let limit = 10000000 let primes = PrimeNumberGenerator limit let start = 1000001 let values = primes.GetPrimesFrom start printfn $"There are {values.Length} prime numbers from {start} to {limit}" with :? NotPrimeException as e -> printfn $"{e.NonPrime} is not prime" printfn $"{e}" printfn "--------" let domain = AppDomain.CreateDomain "Domain2" let gen = domain.CreateInstanceAndUnwrap( typeof<PrimeNumberGenerator>.Assembly.FullName, "PrimeNumberGenerator", true, BindingFlags.Default, null, [| box 1000000 |], null, null) :?> PrimeNumberGenerator let start = 100 printfn $"{gen.GetPrimesFrom start}" with :? NotPrimeException as e -> printfn $"{e.NonPrime} is not prime" printfn $"{e}" printfn "--------" Imports System.Reflection Module Example Sub Main() Dim limit As Integer = 10000000 Dim primes As New PrimeNumberGenerator(limit) Dim start As Integer = 1000001 Dim values() As Integer = primes.GetPrimesFrom(start) Console.WriteLine("There are {0} prime numbers from {1} to {2}", start, limit) Catch e As NotPrimeException Console.WriteLine("{0} is not prime", e.NonPrime) Console.WriteLine(e) Console.WriteLine("--------") End Try Dim domain As AppDomain = AppDomain.CreateDomain("Domain2") Dim gen As PrimeNumberGenerator = domain.CreateInstanceAndUnwrap( GetType(Example).Assembly.FullName, "PrimeNumberGenerator", True, BindingFlags.Default, Nothing, {1000000}, Nothing, Nothing) start = 100 Console.WriteLine(gen.GetPrimesFrom(start)) Catch e As NotPrimeException Console.WriteLine("{0} is not prime", e.NonPrime) Console.WriteLine(e) Console.WriteLine("--------") End Try End Sub End Module ' The example displays the following output: ' 1000001 is not prime ' NotPrimeException: 1000001 is not a prime number. ' at PrimeNumberGenerator.GetPrimesFrom(Int32 prime) ' at Example.Main() ' -------- ' 100 is not prime ' NotPrimeException: 100 is not a prime number. ' at PrimeNumberGenerator.GetPrimesFrom(Int32 prime) ' at Example.Main() ' --------

    Windows 运行时和.NET Framework 4.5.1

    在适用于 Windows 8 的 Windows 8.x Microsoft Store 应用中,当通过非.NET Framework堆栈帧传播异常时,通常会丢失一些异常信息。 从 .NET Framework 4.5.1 和 Windows 8.1 开始,公共语言运行时将继续使用引发的原始Exception对象,除非在非.NET Framework堆栈帧中修改了该异常。

  •