Throw.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. namespace System.Linq
  6. {
  7. public static partial class EnumerableEx
  8. {
  9. /// <summary>
  10. /// Returns a sequence that throws an exception upon enumeration.
  11. /// </summary>
  12. /// <typeparam name="TResult">Result sequence element type.</typeparam>
  13. /// <param name="exception">Exception to throw upon enumerating the resulting sequence.</param>
  14. /// <returns>Sequence that throws the specified exception upon enumeration.</returns>
  15. public static IEnumerable<TResult> Throw<TResult>(Exception exception)
  16. {
  17. if (exception == null)
  18. {
  19. throw new ArgumentNullException(nameof(exception));
  20. }
  21. return ThrowCore<TResult>(exception);
  22. }
  23. private static IEnumerable<TResult> ThrowCore<TResult>(Exception exception)
  24. {
  25. throw exception;
  26. #pragma warning disable 0162
  27. yield break;
  28. #pragma warning restore 0162
  29. }
  30. }
  31. }