Creating and throwing an exception object : Exception Throw « Language Basics « C# / CSharp Tutorial






using System;

class MainClass
{
  public static void Main()
  {
    try
    {
      Exception myException = new Exception("myException");

      myException.HelpLink = "See the Readme.txt file";
      myException.Source = "My Program";
      throw myException;
    }
    catch (Exception e)
    {
      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);
    }
  }
}
HelpLink = See the Readme.txt file
Message = myException
Source = My Program
StackTrace =    at MainClass.Main()
TargetSite = Void Main()








1.22.Exception Throw
1.22.1.Manually throw an exception.
1.22.2.Rethrow an exception
1.22.3.Throw Exception from a function
1.22.4.Wrap exception in another one, adding additional context
1.22.5.Throw Exception in finally statement
1.22.6.Creating and throwing an exception object