Table of contents Home page
using System; public class WeekDayException : Exception { public WeekDayException() : base("A weekday number must be greater than 0 and less than 8.") { } } public class WeekDay { private byte _day; public byte day { get { return _day; } set { if (value > 0 && value < 8) { this._day = value; } else { throw new WeekDayException(); } } } public WeekDay(byte day) { this.day = day; } } public class Program { public static void Main() { try { WeekDay wd = new WeekDay(3); Console.WriteLine("Weekday = {0}", wd.day); wd.day = 5; Console.WriteLine("Weekday = {0}", wd.day); wd.day = 8; // Exception } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadKey(); } }