Manual Exception Object Message C# with Example



Manual Exception Object Message C# with Example

 		using System;

		namespace CSharpClass{
		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)
						throw new Exception(string.Format("Hour is {0}, which can't be more then 23", h));
					else{
						hour = h;
						minute = m;
					}
				}
				catch(Exception exp){
					Console.WriteLine(exp.Message);
				}
			}
			
			public void DisplayTime()
			{
				Console.WriteLine(hour + ":" + minute);
			}
		}
			
			class UsingWatchConstructor{
				static void Main(String[] arg){
					Watch hmt = new Watch();
					hmt.SetTime(30,10);
					Console.Write("Time of the HMT Watch is: ");
					hmt.DisplayTime();
				}
			}
		}
 

0 Comment's

Comment Form

Submit Comment