2010-01-18 37 views
26

Tôi đã tự hỏi nếu có một danh sách với tất cả các loại ngoại lệ. Tôi biết một vài ngoại lệ, nhưng tôi không biết tất cả. Đôi khi tôi ném một ngoại lệ và sau đó tôi nghĩ, có thể. NET đã có một ngoại lệ cho việc này.C# Có tổng quan ngoại lệ không?

Ví dụ: bây giờ tôi cần ngoại lệ cho biết rằng quy trình không tồn tại (như tệp).

Vì vậy, câu hỏi của tôi là: Có ai biết tìm danh sách tất cả Ngoại lệ không? Tôi đã không tìm thấy nó.

Trả lời

20

Trước hết bạn phải hiểu được những gì ngoại lệ và làm thế nào để đối phó với nó. Có một số tài nguyên, có thể giúp bạn hiểu chủ đề này.

  1. "Chọn loại ngoại lệ phù hợp để ném" bởi Krzysztof Cwalina.http://blogs.msdn.com/kcwalina/archive/2006/07/05/657268.aspx

  2. "Cách thiết kế phân cấp ngoại lệ" của Krzysztof Cwalina. http://blogs.msdn.com/kcwalina/archive/2007/01/30/ExceptionHierarchies.aspx

  3. Chế độ ngoại lệ của Chris Brumme. http://blogs.msdn.com/cbrumme/archive/2003/10/01/51524.aspx

Có thể hữu ích:

  1. "Tại sao catch (Exception)/trống bắt là xấu" bởi CLR Team Blog. http://blogs.msdn.com/b/dotnet/archive/2009/02/19/why-catch-exception-empty-catch-is-bad.aspx

  2. "Viết mã xử lý ngoại lệ mạnh mẽ" của Bill Wagner. http://visualstudiomagazine.com/articles/2007/06/01/write-robust-exceptionhandling-code.aspx

  3. "C#: Chúng ta cần phải ngoại lệ kiểm tra trong C#" https://blogs.msdn.com/abhinaba/archive/2005/12/16/504373.aspx

Cũng Jeffrey Richter trong CLR cuốn sách của ông qua C# build ngoại lệ thứ bậc (p.430, Chương 19) và gần đây ông đã viết một chương trình hiển thị tất cả các lớp học mà cuối cùng có nguồn gốc từ System.Exception:

using System; 
using System.Text; 
using System.Reflection; 
using System.Collections.Generic; 
public static class Program 
{ 
    public static void Main() 
    { 
     // Explicitly load the assemblies that we want to reflect over 
     LoadAssemblies(); 
     // Initialize our counters and our exception type list 
     Int32 totalPublicTypes = 0, totalExceptionTypes = 0; 
     List<String> exceptionTree = new List<String>(); 
     // Iterate through all assemblies loaded in this AppDomain 
     foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) 
     { 
      // Iterate through all types defined in this assembly 
      foreach (Type t in a.GetExportedTypes()) 
      { 
       totalPublicTypes++; 
       // Ignore type if not a public class 
       if (!t.IsClass || !t.IsPublic) continue; 
       // Build a string of the type's derivation hierarchy 
       StringBuilder typeHierarchy = new StringBuilder(t.FullName, 5000); 
       // Assume that the type is not an Exception-derived type 
       Boolean derivedFromException = false; 
       // See if System.Exception is a base type of this type 
       Type baseType = t.BaseType; 
       while ((baseType != null) && !derivedFromException) 
       { 
        // Append the base type to the end of the string 
        typeHierarchy.Append("-" + baseType); 
        derivedFromException = (baseType == typeof(System.Exception)); 
        baseType = baseType.BaseType; 
       } 
       // No more bases and not Exception-derived, try next type 
       if (!derivedFromException) continue; 
       // We found an Exception-derived type 
       totalExceptionTypes++; 
       // For this Exception-derived type, 
       // reverse the order of the types in the hierarchy 
       String[] h = typeHierarchy.ToString().Split('-'); 
       Array.Reverse(h); 
       // Build a new string with the hierarchy in order 
       // from Exception -> Exception-derived type 
       // Add the string to the list of Exception types 
       exceptionTree.Add(String.Join("-", h, 1, h.Length - 1)); 
      } 
     } 
     // Sort the Exception types together in order of their hierarchy 
     exceptionTree.Sort(); 
     // Display the Exception tree 
     foreach (String s in exceptionTree) 
     { 
      // For this Exception type, split its base types apart 
      string[] x = s.Split('-'); 
      // Indent based on the number of base types 
      // and then show the most-derived type 
      Console.WriteLine(new String(' ', 3 * x.Length) + x[x.Length - 1]); 
     } 
     // Show final status of the types considered 
     Console.WriteLine("\n---> of {0} types, {1} are " + 
     "derived from System.Exception.", 
     totalPublicTypes, totalExceptionTypes); 
    } 
    private static void LoadAssemblies() 
    { 
     String[] assemblies = { 
       "System, PublicKeyToken={0}", 
       "System.Data, PublicKeyToken={0}", 
       "System.Design, PublicKeyToken={1}", 
       "System.DirectoryServices, PublicKeyToken={1}", 
       "System.Drawing, PublicKeyToken={1}", 
       "System.Drawing.Design, PublicKeyToken={1}", 
       "System.Management, PublicKeyToken={1}", 
       "System.Messaging, PublicKeyToken={1}", 
       "System.Runtime.Remoting, PublicKeyToken={0}", 
       "System.Security, PublicKeyToken={1}", 
       "System.ServiceProcess, PublicKeyToken={1}", 
       "System.Web, PublicKeyToken={1}", 
       "System.Web.RegularExpressions, PublicKeyToken={1}", 
       "System.Web.Services, PublicKeyToken={1}", 
       "System.Windows.Forms, PublicKeyToken={0}", 
       "System.Xml, PublicKeyToken={0}", 
       }; 
     String EcmaPublicKeyToken = "b77a5c561934e089"; 
     String MSPublicKeyToken = "b03f5f7f11d50a3a"; 
     // Get the version of the assembly containing System.Object 
     // We'll assume the same version for all the other assemblies 
     Version version = 
     typeof(System.Object).Assembly.GetName().Version; 
     // Explicitly load the assemblies that we want to reflect over 
     foreach (String a in assemblies) 
     { 
      String Assemblyldentity = 
      String.Format(a, EcmaPublicKeyToken, MSPublicKeyToken) + 
      ", Culture=neutral, Version=" + version; 
      Assembly.Load(AssemblyIdentity); 
     } 
    } 
} 
+0

Liên kết 1 trong danh sách "Có thể hữu ích" đã chết. Đây là phiên bản trực tiếp: http://blogs.msdn.com/b/dotnet/archive/2009/02/19/why-catch-exception-empty-catch-is-bad.aspx –

+0

@Matt post đã được chỉnh sửa với liên kết bạn đã cung cấp. Nắm bắt tốt! –

8

Có một số Exception Hierarchy.

Ngoài ra, MSDN có một hệ thống phân cấp kế thừa tại trang cho Exception class. Nhưng đó chỉ là một danh sách dài và không cung cấp nhiều chi tiết.

Nói chung, .NET dường như có khá ít ngoại lệ tích hợp chung.

2

Bạn có thể tìm tất cả các trường hợp ngoại lệ được xác định trong các kiểu bắt nguồn của System.Exception trong MSDN page cho System.Exception (tìm nó trong phần Hierarchy Inheritance).

6

Một cách hay để xem tất cả các loại có nguồn gốc từ System.Exception trong khuôn khổ .NET bằng cách sử dụng Reflector.

  1. Loại F3 để tìm kiếm cho 'System.Exception'
  2. Chọn 'System.Exception' Loại
  3. Mở rộng 'loại nguồn gốc' nút cây.

Lưu ý rằng Reflector cho phép bạn thêm thực tế bất kỳ bộ sưu tập .NET nào có nghĩa là nó sẽ tìm kiếm các kiểu bắt nguồn của System.Exception trong bất kỳ tập hợp tùy chỉnh nào mà bạn cung cấp. Các khung công tác .NET framework phổ biến nhất được thêm vào theo mặc định.

8

FYI,

Nếu bạn đang sử dụng Visual Studio 2008, bạn vào menu Debug/Exceptions, bạn có thể xem tất cả các trường hợp ngoại lệ trong trường hợp ngoại lệ CLR

  • C++ Exceptions
  • Managed Hỗ trợ gỡ lỗi
  • và kiểm tra Native RunTime.

Với thiết lập này, bạn có thể sắp xếp phải làm gì khi một trong những ngoại lệ xảy ra

Kiểm tra này ra http://explodingcoder.com/cms/content/visual-studio-fail-how-not-debug-net-exception-handling

Các vấn đề liên quan