Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Is there an Exception overview?

Tags:

c#

.net

exception

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.

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.

like image 775
Martijn Avatar asked Jan 18 '10 11:01

Martijn


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

" " C is a computer programming language. That means that you can use C to create lists of instructions for a computer to follow. C is one of thousands of programming languages currently in use.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C language w3schools?

C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


6 Answers

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. "Choosing the Right Type of Exception to Throw" by Krzysztof Cwalina. Link

  2. "How to Design Exception Hierarchies" by Krzysztof Cwalina. Link

  3. The Exception Mode by Chris Brumme. Link

May be helpful:

  1. "Why catch(Exception)/empty catch is bad" by CLR Team Blog. Link

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

  3. "C#: Do we need checked exception in C#" Link

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);
        }
    }
}
like image 163
Sergey Teplyakov Avatar answered Oct 06 '22 00:10

Sergey Teplyakov


There is an Exception Hierarchy.

Also, MSDN has an inheritance hierarchy at the page for the Exception class. But that one's just a long list and doesn't provide much detail.

Generally, .NET seems to have pretty few general built-in exceptions.

like image 35
Joey Avatar answered Oct 05 '22 23:10

Joey


FYI ,

If you are using Visual Studio 2008 , go to menu Debug/Exceptions , you can see all the Exceptions within the

  • CLR Exceptions
  • C++ Exceptions
  • Managed Debugging Assistance
  • and also Native RunTime checks.

With that settings , you can arrange what to do when one of the exception occurs

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

like image 43
Bahadir Cambel Avatar answered Oct 05 '22 22:10

Bahadir Cambel


A good way to see all types that derive from System.Exception in the .NET framework is by using Reflector.

  1. Type F3 to search for 'System.Exception'
  2. Select the 'System.Exception' Type
  3. Expand the 'Derived Types' tree node.

Note that Reflector allows you to dinamically add any .NET assembly meaning that it will search for System.Exception derived types in any custom set of assemblies you provide. The most common .NET framework assemblies are added by default.

like image 43
André Pena Avatar answered Oct 05 '22 22:10

André Pena


The Visual Studio Code Analysis (ie FxCop) documentation lists some general guidance on throwing existing exceptions.

Do not raise reserved exception types

like image 34
Greg Avatar answered Oct 05 '22 22:10

Greg


You can find all the defined exceptions under the derived types of System.Exception in the MSDN page for System.Exception (find it under the Inheritance Hierarchy section).

like image 30
Doctor Blue Avatar answered Oct 06 '22 00:10

Doctor Blue