illustrates a custom exception

image_pdfimage_print

   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example13_9.cs illustrates a custom exception
*/

using System;


// declare the CustomException class
class CustomException : ApplicationException
{

  public CustomException(string Message) : base(Message)
  {

    // set the HelpLink and Source properties
    this.HelpLink = "See the Readme.txt file";
    this.Source = "My Example13_9 Program";

  }

}


public class Example13_9
{

  public static void Main()
  {

    try
    {

      // throw a new CustomException object
      Console.WriteLine("Throwing a new CustomException object");
      throw new CustomException("My CustomException message");

    }
    catch (CustomException e)
    {

      // display the CustomException object's properties
      Console.WriteLine("HelpLink = " + e.HelpLink);
      Console.WriteLine("Message = " + e.Message);
      Console.WriteLine("Source = " + e.Source);
      Console.WriteLine("StackTrace = " + e.StackTrace);
      Console.WriteLine("TargetSite = " + e.TargetSite);

    }

  }

}