Skip to content

Commit

Permalink
Favour returning exceptions rather than throwing in helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonHanna committed Nov 30, 2017
1 parent 50444da commit bd69cc5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions SpookilySharp/ExceptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,30 @@ public static void CheckNotNull(object arg, string name)

public static void CheckNotNullString(string arg) => CheckNotNull(arg, "s");

public static void BadHashCode128Format() =>
throw new FormatException("The string did not contain a 32-digit hexadecimal number.");
public static Exception BadHashCode128Format() =>
new FormatException("The string did not contain a 32-digit hexadecimal number.");

private static void StartIndexOutOfRange() => throw new ArgumentOutOfRangeException("startIndex");
private static Exception StartIndexOutOfRange() => new ArgumentOutOfRangeException("startIndex");

private static void NegativeLength() => throw new ArgumentOutOfRangeException("length");
private static Exception NegativeLength() => new ArgumentOutOfRangeException("length");

private static void PastArrayBounds() => throw new ArgumentException("Attempt to read beyond the end of the array.");
private static Exception PastArrayBounds() => new ArgumentException("Attempt to read beyond the end of the array.");

private static void PastStringBounds() => throw new ArgumentException("Attempt to read beyond the end of the string.");
private static Exception PastStringBounds() => new ArgumentException("Attempt to read beyond the end of the string.");

private static void CheckNotNegativeLength(int length)
{
if (length < 0)
{
NegativeLength();
throw NegativeLength();
}
}

private static void CheckIndexInRange(int startIndex, int length)
{
if ((uint)startIndex >= (uint)length)
{
StartIndexOutOfRange();
throw StartIndexOutOfRange();
}
}

Expand All @@ -65,7 +65,7 @@ public static void CheckArray<T>(T[] message, int startIndex, int length)
CheckIndexInRange(startIndex, len);
if (startIndex + length > len)
{
PastArrayBounds();
throw PastArrayBounds();
}
}

Expand All @@ -82,7 +82,7 @@ public static void CheckBounds(string message, int startIndex, int length)
CheckIndexInRange(startIndex, len);
if (startIndex + length > len)
{
PastStringBounds();
throw PastStringBounds();
}
}

Expand Down
2 changes: 1 addition & 1 deletion SpookilySharp/HashCode128.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public static HashCode128 Parse(string s)
ExceptionHelper.CheckNotNullString(s);
if (!TryParse(s, out HashCode128 ret))
{
ExceptionHelper.BadHashCode128Format();
throw ExceptionHelper.BadHashCode128Format();
}

return ret;
Expand Down

0 comments on commit bd69cc5

Please sign in to comment.