web analytics

Exception Properties in C#

Options

codeling 1599 - 6654
@2018-02-22 10:21:36

Exceptions can be thrown by your C# application or the runtime. Your application uses the try and catch blocks to structure our error handling.

using System;

class Program
{
    static void Main()
    {
        try
        {
            int value = 1 / int.Parse("0");
            Console.WriteLine(value);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

Output

Attempted to divide by zero.
Threre are a lot properties you can read to determine what happned when an exception is thrown.

HelpLink: This is empty because it was not defined on the exception. HelpLink is a string property.

Message: This is a short description of the exception's cause. Message is a read-only string property.

Source: This is the application name. Source is a string property that can be assigned to or read from.

StackTrace: This is the path through the compiled program's method hierarchy that the exception was generated from.

TargetSite: This is the name of the method where the error occurred. This property helps simplify what part of the errors are recorded.



using System;

class Program
{
    static void Main()
    {
        try
        {
            int value = 1 / int.Parse("0");
        }
        catch (Exception ex)
        {
            Console.WriteLine("HelpLink = {0}", ex.HelpLink);
            Console.WriteLine("Message = {0}", ex.Message);
            Console.WriteLine("Source = {0}", ex.Source);
            Console.WriteLine("StackTrace = {0}", ex.StackTrace);
            Console.WriteLine("TargetSite = {0}", ex.TargetSite);
        }
    }
}

Output: truncated

HelpLink =
Message = Attempted to divide by zero.
Source = ConsoleApplication1
StackTrace =    at Program.Main() in C:\...\Program.cs:line 9
TargetSite = Void Main()
@2018-02-22 10:29:12

Data Property

It is possible to store structured data on an exception that is thrown in one part of your program, and then later read in this data. With Data we store associate keys and values. In the following example,  a new exception is allocated and then some value is assigned to the Data property.

using System;
using System.Collections;

class Program
{
    static void Main()
    {
        try
        {
            // Create new exception.
            var ex = new DivideByZeroException("Message");
            // Set the data dictionary.
            ex.Data["Time"] = DateTime.Now;
            ex.Data["Flag"] = true;
            // Throw it.
            throw ex;
        }
        catch (Exception ex)
        {
            // Display the exception's data dictionary.
            foreach (DictionaryEntry pair in ex.Data)
            {
                Console.WriteLine("{0} = {1}", pair.Key, pair.Value);
            }
        }
    }
}

Output

Time = 12/9/2018 10:10:10 AM
Flag = True

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com