C#是否有例外概述?

Mar*_*ijn 27 .net c# exception

我想知道是否有一个包含所有异常类型的列表.我知道一些例外,但我不知道所有这些例外情况.有时候我抛出一个Exception,然后我想,也许.NET已经有了这个例外.

例如,现在我需要一个Exception,表示进程不存在(如文件).

所以我的问题是:有人知道找到所有例外的清单吗?我没找到.

Ser*_*kov 22

首先,您必须了解什么是例外以及如何处理它.有一些资源可以帮助您理解这个主题.

  1. Krzysztof Cwalina"选择正确的例外类型".http://blogs.msdn.com/kcwalina/archive/2006/07/05/657268.aspx

  2. Krzysztof Cwalina撰写的"如何设计例外层次结构".http://blogs.msdn.com/kcwalina/archive/2007/01/30/ExceptionHierarchies.aspx

  3. Chris Brumme的例外模式.http://blogs.msdn.com/cbrumme/archive/2003/10/01/51524.aspx

可能会有所帮助:

  1. CLR团队博客"为什么捕获(异常)/空捕获是坏的".http://blogs.msdn.com/b/dotnet/archive/2009/02/19/why-catch-exception-empty-catch-is-bad.aspx

  2. Bill Wagner撰写的"编写强大的异常处理代码".http://visualstudiomagazine.com/articles/2007/06/01/write-robust-exceptionhandling-code.aspx

  3. "C#:我们是否需要在C#中检查异常" https://blogs.msdn.com/abhinaba/archive/2005/12/16/504373.aspx

Jeffrey Richter在他的书CLR中通过C#构建异常层次结构(p.430,第19章),最近他编写了一个程序,显示最终从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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Joe*_*oey 9

有一个异常层次结构.

此外,MSDN在Exception类的页面上有一个继承层次结构.但那个只是一个很长的清单,并没有提供太多的细节.

通常,.NET似乎很少有一般的内置异常.


Bah*_*bel 8

仅供参考,

如果您使用的是Visual Studio 2008,请转到菜单Debug/Exceptions,您可以看到其中的所有异常

  • CLR例外
  • C++例外
  • 托管调试协助
  • 还有Native RunTime检查.

使用该设置,您可以安排在发生异常时执行的操作

看看http://explodingcoder.com/cms/content/visual-studio-fail-how-not-debug-net-exception-handling


And*_*ena 6

查看.NET框架中从System.Exception派生的所有类型的好方法是使用Reflector.

  1. 键入F3以搜索"System.Exception"
  2. 选择'System.Exception'类型
  3. 展开" 派生类型 "树节点.

请注意,Reflector允许您以动态方式添加任何.NET程序集,这意味着它将在您提供的任何自定义程序集集中搜索System.Exception派生类型.默认情况下会添加最常见的.NET框架程序集.