#858 – Reading Exception Information in a Handler

When you catch an exception in a block of code that serves as an exception handler, you can read information about the error that occurred from an Exception object.  Every exception object will derive from the System.Exception class.  So, at a minimum, you’ll be able to access the properties of System.Exception.  (Not all properties will always contain values, however).

In the example below, the Bark method throws an exception of type System.Exception.  We query the System.Exception instance and dump out some of its values.

                try
                {
                    Dog d = new Dog("Kirby", 15);
                    d.Bark("Woof");
                }
                catch (System.Exception exc)
                {
                    Console.WriteLine(string.Format("Type = {0}", exc.GetType()));    // System.Exception
                    Console.WriteLine(string.Format("Message = {0}", exc.Message));   // Basic message
                    Console.WriteLine(string.Format("Source = {0}", exc.Source));     // Originating assembly
                    Console.WriteLine("Stack:");             // Dump out stack
                    Console.WriteLine(exc.StackTrace);
                }

Note that the call stack shows that Program.Main called Dog.Bark.
858-001

#857 – What an Exception Object Contains

When some code throws an exception, it creates an instance of System.Exception or one of its derived classes and populates the exception object with information that might help understand what went wrong.  The runtime environment also writes some information into the exception object.

In an exception handler, you’ll have access to the information stored within the Exception object.  Listed below are the properties of this type that you can use to get information about the exception.

Always populated:

  • Message – A message summarizing the error
  • StackTrace – A multiline string, describing the current call stack, at the time of the error
  • Source – Name of the assembly where the error originated
  • TargetSite – Information about the method that threw the exception (MethodBase)

Sometimes populated:

  • InnerException – An earlier exception that triggered the current exception  (optional)
  • Data – A series of keyword/value pairs, with additional information about the error  (optional)
  • HelpLink – Link to associated help file  (optional)

#853 – An Exception Is an Instance of the System.Exception Class

When an exception is thrown, the method throwing the exception creates an instance of the System.Exception class and populates it with information about what went wrong.  This Exception object then travels up the call stack until a method that handles the exception is found.  This method “catches” the exception and can then query the Exception object to get information about the exception.

The exception must either be an instance of System.Exception or inherit (directly or indeirectly) from System.Exception.  A more specialized exception class is used when the method throwing the exception wants to include information in the exception object that is specific to the error that occurred.

For example, a method trying to read a file might throw a System.IO.FileNotFoundException.  This class inherits from System.IO.IOException, which inherits from System.SystemException, which in turn inherits from System.Exception.

For example:

853-001