Tuesday, 3 September 2013

Exception handle

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
    (StackTrace was abbreviated for display.)

HelpLink =
Message = Attempted to divide by zero.
Source = ConsoleApplication1
StackTrace =    at Program.Main() in C:\...\Program.cs:line 9
TargetSite = Void Main()

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.

No comments:

Post a Comment