sashomaga

Custom exception class

Mar 10th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1. class InvalidRangeException<T> : ApplicationException
  2. {
  3.     #region Fields
  4.     private T min;
  5.     private T max;
  6.     #endregion
  7.  
  8.     #region Properties
  9.     public T Min
  10.     {
  11.         get { return this.min;}
  12.         private set { this.min = value;}
  13.     }
  14.     public T Max
  15.     {
  16.         get { return this.max; }
  17.         private set { this.max = value; }
  18.     }
  19.     #endregion
  20.  
  21.     #region Methods
  22.     public override string Message
  23.     {
  24.         get
  25.         {
  26.             return string.Format("Value is out of range {0} - {1}",this.min,this.max);
  27.         }
  28.     }
  29.     #endregion
  30.  
  31.     #region Constructors
  32.     public InvalidRangeException(T min, T max)
  33.     {
  34.         this.min = min;
  35.         this.max = max;
  36.     }
  37.     #endregion
  38. }
  39.  
  40. static void Main()
  41.     {
  42.         try
  43.         {
  44.             //int
  45.             Console.WriteLine("Enter value between 1 and 100");
  46.             int inputNum = int.Parse(Console.ReadLine());
  47.             if (inputNum < 1 || inputNum > 100)
  48.             {
  49.                 throw new InvalidRangeException<int>(1, 100);
  50.             }
  51.             //datetime
  52.             Console.WriteLine("Enter date between 1.1.1980 and 31.12.2013");
  53.             DateTime minDate = new DateTime(1980,1,1);
  54.             DateTime maxDate = new DateTime(2013,12,31);
  55.             DateTime inputData = DateTime.Parse(Console.ReadLine());
  56.             if (inputData < minDate || inputData > maxDate)
  57.             {
  58.                 throw new InvalidRangeException<DateTime>(minDate, maxDate);
  59.             }
  60.  
  61.         }
  62.         catch (InvalidRangeException<int> err)
  63.         {
  64.             Console.WriteLine(err.Message);
  65.         }
  66.         catch (InvalidRangeException<DateTime> err)
  67.         {
  68.             Console.WriteLine(err.Message);
  69.         }
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment