// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Runtime.InteropServices; using System.Threading; namespace CommunityToolkit.Diagnostics; /// /// Helper methods to efficiently throw exceptions. /// public static partial class ThrowHelper { /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArrayTypeMismatchException() { throw new ArrayTypeMismatchException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArrayTypeMismatchException(string? message) { throw new ArrayTypeMismatchException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArrayTypeMismatchException(string? message, Exception? innerException) { throw new ArrayTypeMismatchException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentException() { throw new ArgumentException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentException(string? message) { throw new ArgumentException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentException(string? message, Exception? innerException) { throw new ArgumentException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// The message to include in the exception. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentException(string? name, string? message) { throw new ArgumentException(message, name); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentException(string? name, string? message, Exception? innerException) { throw new ArgumentException(message, name, innerException); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentNullException() { throw new ArgumentNullException(); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentNullException(string? name) { throw new ArgumentNullException(name); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// The inner to include. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentNullException(string? name, Exception? innerException) { throw new ArgumentNullException(name, innerException); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// The message to include in the exception. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentNullException(string? name, string? message) { throw new ArgumentNullException(name, message); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentOutOfRangeException() { throw new ArgumentOutOfRangeException(); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentOutOfRangeException(string? name) { throw new ArgumentOutOfRangeException(name); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentOutOfRangeException(string? name, Exception? innerException) { throw new ArgumentOutOfRangeException(name, innerException); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// The message to include in the exception. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentOutOfRangeException(string? name, string? message) { throw new ArgumentOutOfRangeException(name, message); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// The current argument value. /// The message to include in the exception. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowArgumentOutOfRangeException(string? name, object? value, string? message) { throw new ArgumentOutOfRangeException(name, value, message); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowCOMException() { throw new COMException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowCOMException(string? message) { throw new COMException(message); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// The inner to include. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowCOMException(string? message, Exception? innerException) { throw new COMException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// The HRESULT of the errror to include. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowCOMException(string? message, int error) { throw new COMException(message, error); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowExternalException() { throw new ExternalException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowExternalException(string? message) { throw new ExternalException(message); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// The inner to include. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowExternalException(string? message, Exception? innerException) { throw new ExternalException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// The argument name. /// The HRESULT of the errror to include. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowExternalException(string? message, int error) { throw new ExternalException(message, error); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowFormatException() { throw new FormatException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowFormatException(string? message) { throw new FormatException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowFormatException(string? message, Exception? innerException) { throw new FormatException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowInsufficientMemoryException() { throw new InsufficientMemoryException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowInsufficientMemoryException(string? message) { throw new InsufficientMemoryException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowInsufficientMemoryException(string? message, Exception? innerException) { throw new InsufficientMemoryException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowInvalidDataException() { throw new InvalidDataException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowInvalidDataException(string? message) { throw new InvalidDataException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowInvalidDataException(string? message, Exception? innerException) { throw new InvalidDataException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowInvalidOperationException() { throw new InvalidOperationException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowInvalidOperationException(string? message) { throw new InvalidOperationException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowInvalidOperationException(string? message, Exception? innerException) { throw new InvalidOperationException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowLockRecursionException() { throw new LockRecursionException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowLockRecursionException(string? message) { throw new LockRecursionException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowLockRecursionException(string? message, Exception? innerException) { throw new LockRecursionException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingFieldException() { throw new MissingFieldException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingFieldException(string? message) { throw new MissingFieldException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingFieldException(string? message, Exception? innerException) { throw new MissingFieldException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// The target class being inspected. /// The target field being retrieved. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingFieldException(string? className, string? fieldName) { throw new MissingFieldException(className, fieldName); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingMemberException() { throw new MissingMemberException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingMemberException(string? message) { throw new MissingMemberException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingMemberException(string? message, Exception? innerException) { throw new MissingMemberException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// The target class being inspected. /// The target member being retrieved. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingMemberException(string? className, string? memberName) { throw new MissingMemberException(className, memberName); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingMethodException() { throw new MissingMethodException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingMethodException(string? message) { throw new MissingMethodException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingMethodException(string? message, Exception? innerException) { throw new MissingMethodException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// The target class being inspected. /// The target method being retrieved. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowMissingMethodException(string? className, string? methodName) { throw new MissingMethodException(className, methodName); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowNotSupportedException() { throw new NotSupportedException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowNotSupportedException(string? message) { throw new NotSupportedException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowNotSupportedException(string? message, Exception? innerException) { throw new NotSupportedException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// The name of the disposed object. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowObjectDisposedException(string? objectName) { throw new ObjectDisposedException(objectName); } /// /// Throws a new . /// /// The type of expected result. /// The name of the disposed object. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowObjectDisposedException(string? objectName, Exception? innerException) { throw new ObjectDisposedException(objectName, innerException); } /// /// Throws a new . /// /// The type of expected result. /// The name of the disposed object. /// The message to include in the exception. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowObjectDisposedException(string? objectName, string? message) { throw new ObjectDisposedException(objectName, message); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowOperationCanceledException() { throw new OperationCanceledException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowOperationCanceledException(string? message) { throw new OperationCanceledException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowOperationCanceledException(string? message, Exception? innerException) { throw new OperationCanceledException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// The in use. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowOperationCanceledException(CancellationToken token) { throw new OperationCanceledException(token); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The in use. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowOperationCanceledException(string? message, CancellationToken token) { throw new OperationCanceledException(message, token); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// The in use. /// Thrown with the specified parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowOperationCanceledException(string? message, Exception? innerException, CancellationToken token) { throw new OperationCanceledException(message, innerException, token); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowPlatformNotSupportedException() { throw new PlatformNotSupportedException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowPlatformNotSupportedException(string? message) { throw new PlatformNotSupportedException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowPlatformNotSupportedException(string? message, Exception? innerException) { throw new PlatformNotSupportedException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowSynchronizationLockException() { throw new SynchronizationLockException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowSynchronizationLockException(string? message) { throw new SynchronizationLockException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowSynchronizationLockException(string? message, Exception? innerException) { throw new SynchronizationLockException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowTimeoutException() { throw new TimeoutException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowTimeoutException(string? message) { throw new TimeoutException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowTimeoutException(string? message, Exception? innerException) { throw new TimeoutException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowUnauthorizedAccessException() { throw new UnauthorizedAccessException(); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowUnauthorizedAccessException(string? message) { throw new UnauthorizedAccessException(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowUnauthorizedAccessException(string? message, Exception? innerException) { throw new UnauthorizedAccessException(message, innerException); } /// /// Throws a new . /// /// The type of expected result. /// Thrown with no parameters. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowWin32Exception() { throw new Win32Exception(); } /// /// Throws a new . /// /// The type of expected result. /// The Win32 error code associated with this exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowWin32Exception(int error) { throw new Win32Exception(error); } /// /// Throws a new . /// /// The type of expected result. /// The Win32 error code associated with this exception. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowWin32Exception(int error, string? message) { throw new Win32Exception(error, message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowWin32Exception(string? message) { throw new Win32Exception(message); } /// /// Throws a new . /// /// The type of expected result. /// The message to include in the exception. /// The inner to include. /// Thrown with the specified parameter. /// This method always throws, so it actually never returns a value. [DoesNotReturn] public static T ThrowWin32Exception(string? message, Exception? innerException) { throw new Win32Exception(message, innerException); } }