Customl Exception C# with Example
using System; namespace CSharpExceptionHandling{ public class WatchException : ApplicationException{ private string messageDetails = String.Empty; public DateTime ErrorTimeStamp {get; set;} public string CauseOfError {get; set;} public WatchException(){} public WatchException(string msg, string cause, DateTime time){ messageDetails = msg; CauseOfError = cause; ErrorTimeStamp = time; } // Override the Exception.Message property. public override string Message{ get{ return string.Format("Watch Error Message: {0}", messageDetails); } } } class Watch { public byte hour{set; get;} public byte minute{set; get;} public Watch(){ hour = 12; minute = 0; } public void SetTime(byte h, byte m) { try{ if(h > 23){ WatchException exp = new WatchException( string.Format("Invalid Time Specified."), string.Format("Hour is {0}, which can't be more then 23", h), DateTime.Now ); exp.HelpLink = "http://www.bccfalna.com/"; throw exp; } else{ hour = h; minute = m; } } catch(WatchException exp){ Console.WriteLine("Error Description: " + exp.Message); Console.WriteLine("Cause of Error: " + exp.CauseOfError); Console.WriteLine("Time of Error: " + exp.ErrorTimeStamp); } } public void DisplayTime() { Console.WriteLine(hour + ":" + minute); } } class UsingWatchException{ static void Main(String[] arg){ Watch hmt = new Watch(); hmt.SetTime(30,10); Console.Write("Time of the HMT Watch is: "); hmt.DisplayTime(); } } }