OverflowException Check in C#.Net

Consider the following code.

Understating the problem

short shortNumber;
int i = 32768;
shortNumber= (short)i;

Here the shortNumber will have -32767. So we will have to tackle these issues.

Reason

It happens because of the Upper bound of short of 32767.

Solution

Use the 'checked' keyword to avoid the overflow

try {
  shortNumber= checked((short)i); // solution
}
catch(OverflowException ex)
 {
}


We have to use the try, catch block

If short is out of range then we will have the OverflowException.and we can use the catch to handle this situation.