Skip to content

Commit 7548771

Browse files
committed
Set default Contract exception to InvalidOperationException
Previously, if `Contract.Requires(bool)` or `Contract.Assumes(bool)` were called without specifying an exception type, then an `Exception` was thrown. This is now updated to throw the more specific `InvalidOperationException`. If implementors want more control over this, they should prefer to call `Contract.Requires<T>(bool)` or `Contract.Assumes<T>(bool)`. As `InvalidOperationException` derives from `Exception`, this shouldn't be a breaking change, but will provide implementors more flexibility in capturing exceptions raised by this library.
1 parent 1f49495 commit 7548771

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

OnTopic/Internal/Diagnostics/Contract.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@ public static class Contract {
5151
| METHOD: REQUIRES
5252
\-------------------------------------------------------------------------------------------------------------------------*/
5353
/// <summary>
54-
/// Will throw a <see cref="Exception"/> if the supplied expression evaluates to false.
54+
/// Will throw a <see cref="InvalidOperationException"/> if the supplied expression evaluates to false.
5555
/// </summary>
5656
/// <param name="isValid">An expression resulting in a boolean value indicating if an exception should be thrown.</param>
5757
/// <param name="errorMessage">Optionally provides an error message in case an exception is thrown.</param>
58-
/// <exception cref="Exception">
58+
/// <exception cref="InvalidOperationException">
5959
/// Thrown when <paramref name="isValid"/> returns <see langword="true"/>.
6060
/// </exception>
61-
public static void Requires(bool isValid, string? errorMessage = null) => Requires<Exception>(isValid, errorMessage);
61+
public static void Requires(bool isValid, string? errorMessage = null) =>
62+
Requires<InvalidOperationException>(isValid, errorMessage);
6263

6364
/// <summary>
6465
/// Will throw an <see cref="ArgumentNullException"/> if the supplied object is <see langword="null"/>.
@@ -118,7 +119,7 @@ or NotSupportedException
118119
| METHOD: ASSUME
119120
\-------------------------------------------------------------------------------------------------------------------------*/
120121
/// <summary>
121-
/// Ensures that a condition is met. If not, an <see cref="Exception"/> is thrown.
122+
/// Ensures that a condition is met. If not, an <see cref="InvalidOperationException"/> is thrown.
122123
/// </summary>
123124
/// <remarks>
124125
/// This is virtually identical to <see cref="Requires(Boolean, String)"/> except that, syntactically, it is expected to
@@ -128,11 +129,11 @@ or NotSupportedException
128129
/// </remarks>
129130
/// <param name="isValid">An expression resulting in a boolean value indicating if an exception should be thrown.</param>
130131
/// <param name="errorMessage">Optionally provides an error message in case an exception is thrown.</param>
131-
/// <exception cref="Exception">
132+
/// <exception cref="InvalidOperationException">
132133
/// Thrown when <paramref name="isValid"/> returns <see langword="true"/>.
133134
/// </exception>
134135
public static void Assume(bool isValid, string? errorMessage = null) =>
135-
Requires<Exception>(isValid, errorMessage);
136+
Requires<InvalidOperationException>(isValid, errorMessage);
136137

137138
/// <summary>
138139
/// Ensures that a condition is met. If not, the provided exception is thrown.

0 commit comments

Comments
 (0)