Skip to content

Parameterless ThrowHelper APIs #3550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 252 additions & 0 deletions Microsoft.Toolkit/Diagnostics/ThrowHelper.Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ namespace Microsoft.Toolkit.Diagnostics
/// </summary>
public static partial class ThrowHelper
{
/// <summary>
/// Throws a new <see cref="ArrayTypeMismatchException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="ArrayTypeMismatchException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowArrayTypeMismatchException<T>()
{
throw new ArrayTypeMismatchException();
}

/// <summary>
/// Throws a new <see cref="ArrayTypeMismatchException"/>.
/// </summary>
Expand Down Expand Up @@ -47,6 +59,18 @@ public static T ThrowArrayTypeMismatchException<T>(string message, Exception inn
throw new ArrayTypeMismatchException(message, innerException);
}

/// <summary>
/// Throws a new <see cref="ArgumentException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="ArgumentException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowArgumentException<T>()
{
throw new ArgumentException();
}

/// <summary>
/// Throws a new <see cref="ArgumentException"/>.
/// </summary>
Expand Down Expand Up @@ -103,6 +127,18 @@ public static T ThrowArgumentException<T>(string name, string message, Exception
throw new ArgumentException(message, name, innerException);
}

/// <summary>
/// Throws a new <see cref="ArgumentNullException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="ArgumentNullException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowArgumentNullException<T>()
{
throw new ArgumentNullException();
}

/// <summary>
/// Throws a new <see cref="ArgumentNullException"/>.
/// </summary>
Expand Down Expand Up @@ -144,6 +180,18 @@ public static T ThrowArgumentNullException<T>(string name, string message)
throw new ArgumentNullException(name, message);
}

/// <summary>
/// Throws a new <see cref="ArgumentOutOfRangeException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="ArgumentOutOfRangeException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowArgumentOutOfRangeException<T>()
{
throw new ArgumentOutOfRangeException();
}

/// <summary>
/// Throws a new <see cref="ArgumentOutOfRangeException"/>.
/// </summary>
Expand Down Expand Up @@ -201,6 +249,18 @@ public static T ThrowArgumentOutOfRangeException<T>(string name, object value, s
}

#if !NETSTANDARD1_4
/// <summary>
/// Throws a new <see cref="COMException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="COMException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowCOMException<T>()
{
throw new COMException();
}

/// <summary>
/// Throws a new <see cref="COMException"/>.
/// </summary>
Expand Down Expand Up @@ -242,6 +302,18 @@ public static T ThrowCOMException<T>(string message, int error)
throw new COMException(message, error);
}

/// <summary>
/// Throws a new <see cref="ExternalException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="ExternalException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowExternalException<T>()
{
throw new ExternalException();
}

/// <summary>
/// Throws a new <see cref="ExternalException"/>.
/// </summary>
Expand Down Expand Up @@ -284,6 +356,18 @@ public static T ThrowExternalException<T>(string message, int error)
}
#endif

/// <summary>
/// Throws a new <see cref="FormatException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="FormatException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowFormatException<T>()
{
throw new FormatException();
}

/// <summary>
/// Throws a new <see cref="FormatException"/>.
/// </summary>
Expand Down Expand Up @@ -312,6 +396,18 @@ public static T ThrowFormatException<T>(string message, Exception innerException
}

#if !NETSTANDARD1_4
/// <summary>
/// Throws a new <see cref="InsufficientMemoryException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="InsufficientMemoryException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowInsufficientMemoryException<T>()
{
throw new InsufficientMemoryException();
}

/// <summary>
/// Throws a new <see cref="InsufficientMemoryException"/>.
/// </summary>
Expand Down Expand Up @@ -340,6 +436,18 @@ public static T ThrowInsufficientMemoryException<T>(string message, Exception in
}
#endif

/// <summary>
/// Throws a new <see cref="InvalidDataException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="InvalidDataException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowInvalidDataException<T>()
{
throw new InvalidDataException();
}

/// <summary>
/// Throws a new <see cref="InvalidDataException"/>.
/// </summary>
Expand Down Expand Up @@ -367,6 +475,18 @@ public static T ThrowInvalidDataException<T>(string message, Exception innerExce
throw new InvalidDataException(message, innerException);
}

/// <summary>
/// Throws a new <see cref="InvalidOperationException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="InvalidOperationException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowInvalidOperationException<T>()
{
throw new InvalidOperationException();
}

/// <summary>
/// Throws a new <see cref="InvalidOperationException"/>.
/// </summary>
Expand Down Expand Up @@ -394,6 +514,18 @@ public static T ThrowInvalidOperationException<T>(string message, Exception inne
throw new InvalidOperationException(message, innerException);
}

/// <summary>
/// Throws a new <see cref="LockRecursionException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="LockRecursionException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowLockRecursionException<T>()
{
throw new LockRecursionException();
}

/// <summary>
/// Throws a new <see cref="LockRecursionException"/>.
/// </summary>
Expand Down Expand Up @@ -421,6 +553,18 @@ public static T ThrowLockRecursionException<T>(string message, Exception innerEx
throw new LockRecursionException(message, innerException);
}

/// <summary>
/// Throws a new <see cref="MissingFieldException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="MissingFieldException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowMissingFieldException<T>()
{
throw new MissingFieldException();
}

/// <summary>
/// Throws a new <see cref="MissingFieldException"/>.
/// </summary>
Expand Down Expand Up @@ -464,6 +608,18 @@ public static T ThrowMissingFieldException<T>(string className, string fieldName
}
#endif

/// <summary>
/// Throws a new <see cref="MissingMemberException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="MissingMemberException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowMissingMemberException<T>()
{
throw new MissingMemberException();
}

/// <summary>
/// Throws a new <see cref="MissingMemberException"/>.
/// </summary>
Expand Down Expand Up @@ -507,6 +663,18 @@ public static T ThrowMissingMemberException<T>(string className, string memberNa
}
#endif

/// <summary>
/// Throws a new <see cref="MissingMethodException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="MissingMethodException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowMissingMethodException<T>()
{
throw new MissingMethodException();
}

/// <summary>
/// Throws a new <see cref="MissingMethodException"/>.
/// </summary>
Expand Down Expand Up @@ -550,6 +718,18 @@ public static T ThrowMissingMethodException<T>(string className, string methodNa
}
#endif

/// <summary>
/// Throws a new <see cref="NotSupportedException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="NotSupportedException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowNotSupportedException<T>()
{
throw new NotSupportedException();
}

/// <summary>
/// Throws a new <see cref="NotSupportedException"/>.
/// </summary>
Expand Down Expand Up @@ -618,6 +798,18 @@ public static T ThrowObjectDisposedException<T>(string objectName, string messag
throw new ObjectDisposedException(objectName, message);
}

/// <summary>
/// Throws a new <see cref="OperationCanceledException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="OperationCanceledException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowOperationCanceledException<T>()
{
throw new OperationCanceledException();
}

/// <summary>
/// Throws a new <see cref="OperationCanceledException"/>.
/// </summary>
Expand Down Expand Up @@ -687,6 +879,18 @@ public static T ThrowOperationCanceledException<T>(string message, Exception inn
throw new OperationCanceledException(message, innerException, token);
}

/// <summary>
/// Throws a new <see cref="PlatformNotSupportedException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="PlatformNotSupportedException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowPlatformNotSupportedException<T>()
{
throw new PlatformNotSupportedException();
}

/// <summary>
/// Throws a new <see cref="PlatformNotSupportedException"/>.
/// </summary>
Expand Down Expand Up @@ -714,6 +918,18 @@ public static T ThrowPlatformNotSupportedException<T>(string message, Exception
throw new PlatformNotSupportedException(message, innerException);
}

/// <summary>
/// Throws a new <see cref="SynchronizationLockException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="SynchronizationLockException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowSynchronizationLockException<T>()
{
throw new SynchronizationLockException();
}

/// <summary>
/// Throws a new <see cref="SynchronizationLockException"/>.
/// </summary>
Expand Down Expand Up @@ -741,6 +957,18 @@ public static T ThrowSynchronizationLockException<T>(string message, Exception i
throw new SynchronizationLockException(message, innerException);
}

/// <summary>
/// Throws a new <see cref="TimeoutException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="TimeoutException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowTimeoutException<T>()
{
throw new TimeoutException();
}

/// <summary>
/// Throws a new <see cref="TimeoutException"/>.
/// </summary>
Expand Down Expand Up @@ -768,6 +996,18 @@ public static T ThrowTimeoutException<T>(string message, Exception innerExceptio
throw new TimeoutException(message, innerException);
}

/// <summary>
/// Throws a new <see cref="UnauthorizedAccessException"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="UnauthorizedAccessException">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowUnauthorizedAccessException<T>()
{
throw new UnauthorizedAccessException();
}

/// <summary>
/// Throws a new <see cref="UnauthorizedAccessException"/>.
/// </summary>
Expand Down Expand Up @@ -795,6 +1035,18 @@ public static T ThrowUnauthorizedAccessException<T>(string message, Exception in
throw new UnauthorizedAccessException(message, innerException);
}

/// <summary>
/// Throws a new <see cref="Win32Exception"/>.
/// </summary>
/// <typeparam name="T">The type of expected result.</typeparam>
/// <exception cref="Win32Exception">Thrown with no parameters.</exception>
/// <returns>This method always throws, so it actually never returns a value.</returns>
[DoesNotReturn]
public static T ThrowWin32Exception<T>()
{
throw new Win32Exception();
}

/// <summary>
/// Throws a new <see cref="Win32Exception"/>.
/// </summary>
Expand Down
Loading