Always throw the most specific exception type appropriate for the error condition and include contextual information in the error message. This helps developers quickly identify and fix issues.
Always throw the most specific exception type appropriate for the error condition and include contextual information in the error message. This helps developers quickly identify and fix issues.
Key practices:
PlatformNotSupportedException
instead of NotSupportedException
)OperationCanceledException
and TaskCanceledException
Example:
// Bad
throw new Exception("Operation failed");
// or
throw new CryptographicException("Unknown error (0xc100000d)");
// Good
throw new PlatformNotSupportedException(
SR.Format(SR.ProcessStartSingleFeatureNotSupported, nameof(feature)));
// or
await Assert.ThrowsAnyAsync<OperationCanceledException>(() =>
operation.ExecuteAsync(cancellationToken));
Enter the URL of a public GitHub repository