C#有没有异常概述? [英] C# Is there an Exception overview?

查看:105
本文介绍了C#有没有异常概述?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一个包含所有异常类型的列表。我知道几个例外,但我不认识他们。有时候我会抛出异常,然后我想,也许.NET已经有一个例外。

I was wondering if there's a list with all Exception types. I know a few Exceptions, but I don't know them all. Sometimes I throw an Exception and then I think, maybe .NET already has an Exception for this.

例如,现在我需要一个异常, t存在(像一个文件)。

For example, now I need an Exception that says that a process doesn't exists (like a file).

所以我的问题是:有没有人知道找到所有例外的列表?我没有找到它。

So therefore my question is: Does anybody know to find a list of all Exceptions? I didn't found it.

推荐答案

首先你必须明白什么是异常以及如何处理。
有一些资源,可以帮助您了解这个主题。

First of all you must understand what are exceptions and how to deal with it. There some resources, that can help you to understand this topic.


  1. 选择正确的异常类型投掷由Krzysztof Cwalina。 http://blogs.msdn.com/kcwalina/archive/2006 /07/05/657268.aspx Krzysztof Cwalina的

  1. "Choosing the Right Type of Exception to Throw" by Krzysztof Cwalina. http://blogs.msdn.com/kcwalina/archive/2006/07/05/657268.aspx

如何设计异常层次结构。 http://blogs.msdn.com/kcwalina/archive/2007 /01/30/ExceptionHierarchies.aspx

"How to Design Exception Hierarchies" by Krzysztof Cwalina. http://blogs.msdn.com/kcwalina/archive/2007/01/30/ExceptionHierarchies.aspx

Chris Brumme的异常模式。 http://blogs.msdn.com/cbrumme/archive/2003 /10/01/51524.aspx

The Exception Mode by 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

写鲁棒异常处理代码由Bill Wagner。 http://visualstudiomagazine.com/articles/2007/06 /01/write-robust-exceptionhandling-code.aspx

"Write Robust Exception-Handling Code" by Bill Wagner. http://visualstudiomagazine.com/articles/2007/06/01/write-robust-exceptionhandling-code.aspx

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

"C#: Do we need checked exception in C#" https://blogs.msdn.com/abhinaba/archive/2005/12/16/504373.aspx

Jeffrey Richter在他的书CLR中通过C#构建异常层次结构(第430页,第19章),最近他写了一个程序,显示最终从 System.Exception 派生的所有类:

Also Jeffrey Richter in his book CLR via C# build exception hierarchy (p.430, Chapter 19) and lately he wrote a program that display all of the classes that are ultimately derived from 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);
        }
    }
}

这篇关于C#有没有异常概述?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆