Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class InvalidRangeException<T> : ApplicationException
- {
- #region Fields
- private T min;
- private T max;
- #endregion
- #region Properties
- public T Min
- {
- get { return this.min;}
- private set { this.min = value;}
- }
- public T Max
- {
- get { return this.max; }
- private set { this.max = value; }
- }
- #endregion
- #region Methods
- public override string Message
- {
- get
- {
- return string.Format("Value is out of range {0} - {1}",this.min,this.max);
- }
- }
- #endregion
- #region Constructors
- public InvalidRangeException(T min, T max)
- {
- this.min = min;
- this.max = max;
- }
- #endregion
- }
- static void Main()
- {
- try
- {
- //int
- Console.WriteLine("Enter value between 1 and 100");
- int inputNum = int.Parse(Console.ReadLine());
- if (inputNum < 1 || inputNum > 100)
- {
- throw new InvalidRangeException<int>(1, 100);
- }
- //datetime
- Console.WriteLine("Enter date between 1.1.1980 and 31.12.2013");
- DateTime minDate = new DateTime(1980,1,1);
- DateTime maxDate = new DateTime(2013,12,31);
- DateTime inputData = DateTime.Parse(Console.ReadLine());
- if (inputData < minDate || inputData > maxDate)
- {
- throw new InvalidRangeException<DateTime>(minDate, maxDate);
- }
- }
- catch (InvalidRangeException<int> err)
- {
- Console.WriteLine(err.Message);
- }
- catch (InvalidRangeException<DateTime> err)
- {
- Console.WriteLine(err.Message);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment