I'm performing some parameter validation in a method and throwing an exception where necessary. Do I need to bother manually throwing exceptions of this type? As long as the caller is wrapped in a try..catch block a similar exception is thrown regardless of whether the manual checks are in place.
public static Int16 ToInt16(this byte[] value, int startIndex, bool isBigEndian) {
// are these exceptions necessary?
if (value == null) {
throw new ArgumentNullException("value");
}
if ((startIndex + 1) >= value.Length) {
throw new ArgumentOutOfRangeException("startIndex");
}
return (isBigEndian)
? (Int16)((value[startIndex] << 8) | value[startIndex + 1])
: (Int16)((value[startIndex + 1] << 8) | value[startIndex]);
}
This is an extension method used for converting 2 bytes in an array to an Int16 with a way to toggle Big Endian or Little Endian conversion.