From 80e10aacb252253ed4799b2b90374e732a3bdfa2 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Mon, 5 Sep 2016 01:55:54 +0100 Subject: [PATCH] Improved Exception handling --- src/System.Slices/System/Contract.cs | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/System.Slices/System/Contract.cs b/src/System.Slices/System/Contract.cs index 7f24a0b3e6c..9d84fddccd7 100644 --- a/src/System.Slices/System/Contract.cs +++ b/src/System.Slices/System/Contract.cs @@ -12,15 +12,16 @@ public static void Requires(bool condition) { if (!condition) { - throw NewArgumentException(); + ThrowArgumentException(); } } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void RequiresNonNegative(int n) { if (n < 0) { - throw NewArgumentOutOfRangeException(); + ThrowArgumentOutOfRangeException(); } } @@ -29,7 +30,7 @@ public static void RequiresInRange(int start, uint length) { if ((uint)start >= length) { - throw NewArgumentOutOfRangeException(); + ThrowArgumentOutOfRangeException(); } } @@ -38,7 +39,7 @@ public static void RequiresInRange(uint start, uint length) { if (start >= length) { - throw NewArgumentOutOfRangeException(); + ThrowArgumentOutOfRangeException(); } } @@ -47,7 +48,7 @@ public static void RequiresInInclusiveRange(int start, uint length) { if ((uint)start > length) { - throw NewArgumentOutOfRangeException(); + ThrowArgumentOutOfRangeException(); } } @@ -56,7 +57,7 @@ public static void RequiresInInclusiveRange(uint start, uint length) { if (start > length) { - throw NewArgumentOutOfRangeException(); + ThrowArgumentOutOfRangeException(); } } @@ -67,7 +68,7 @@ public static void RequiresInInclusiveRange(int start, int length, uint existing || length < 0 || (uint)(start + length) > existingLength) { - throw NewArgumentOutOfRangeException(); + ThrowArgumentOutOfRangeException(); } } @@ -78,21 +79,18 @@ public static void RequiresInInclusiveRange(uint start, uint length, uint existi || length < 0 || (start + length) > existingLength) { - throw NewArgumentOutOfRangeException(); + ThrowArgumentOutOfRangeException(); } } - - [MethodImpl(MethodImplOptions.NoInlining)] - private static Exception NewArgumentException() + private static void ThrowArgumentException() { - return new ArgumentException(); + throw new ArgumentException(); } - [MethodImpl(MethodImplOptions.NoInlining)] - private static Exception NewArgumentOutOfRangeException() + private static void ThrowArgumentOutOfRangeException() { - return new ArgumentOutOfRangeException(); + throw new ArgumentOutOfRangeException(); } } }