Catch.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /// Creates a sequence that corresponds to the source sequence, concatenating it with the sequence resulting from
  11. /// calling an exception handler function in case of an error.
  12. /// </summary>
  13. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  14. /// <typeparam name="TException">Exception type to catch.</typeparam>
  15. /// <param name="source">Source sequence.</param>
  16. /// <param name="handler">Handler to invoke when an exception of the specified type occurs.</param>
  17. /// <returns>Source sequence, concatenated with an exception handler result sequence in case of an error.</returns>
  18. public static IEnumerable<TSource> Catch<TSource, TException>(this IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler)
  19. where TException : Exception
  20. {
  21. if (source == null)
  22. throw new ArgumentNullException(nameof(source));
  23. if (handler == null)
  24. throw new ArgumentNullException(nameof(handler));
  25. return CatchCore(source, handler);
  26. }
  27. /// <summary>
  28. /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
  29. /// </summary>
  30. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  31. /// <param name="sources">Source sequences.</param>
  32. /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
  33. public static IEnumerable<TSource> Catch<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
  34. {
  35. if (sources == null)
  36. throw new ArgumentNullException(nameof(sources));
  37. return CatchCore(sources);
  38. }
  39. /// <summary>
  40. /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
  41. /// </summary>
  42. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  43. /// <param name="sources">Source sequences.</param>
  44. /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
  45. public static IEnumerable<TSource> Catch<TSource>(params IEnumerable<TSource>[] sources)
  46. {
  47. if (sources == null)
  48. throw new ArgumentNullException(nameof(sources));
  49. return CatchCore(sources);
  50. }
  51. /// <summary>
  52. /// Creates a sequence that returns the elements of the first sequence, switching to the second in case of an error.
  53. /// </summary>
  54. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  55. /// <param name="first">First sequence.</param>
  56. /// <param name="second">Second sequence, concatenated to the result in case the first sequence completes exceptionally.</param>
  57. /// <returns>The first sequence, followed by the second sequence in case an error is produced.</returns>
  58. public static IEnumerable<TSource> Catch<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
  59. {
  60. if (first == null)
  61. throw new ArgumentNullException(nameof(first));
  62. if (second == null)
  63. throw new ArgumentNullException(nameof(second));
  64. return CatchCore(new[] { first, second });
  65. }
  66. private static IEnumerable<TSource> CatchCore<TSource, TException>(IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler)
  67. where TException : Exception
  68. {
  69. var err = default(IEnumerable<TSource>);
  70. using (var e = source.GetEnumerator())
  71. {
  72. while (true)
  73. {
  74. var c = default(TSource);
  75. try
  76. {
  77. if (!e.MoveNext())
  78. break;
  79. c = e.Current;
  80. }
  81. catch (TException ex)
  82. {
  83. err = handler(ex);
  84. break;
  85. }
  86. yield return c;
  87. }
  88. }
  89. if (err != null)
  90. {
  91. foreach (var item in err)
  92. {
  93. yield return item;
  94. }
  95. }
  96. }
  97. private static IEnumerable<TSource> CatchCore<TSource>(IEnumerable<IEnumerable<TSource>> sources)
  98. {
  99. var error = default(Exception);
  100. foreach (var source in sources)
  101. {
  102. using var e = source.GetEnumerator();
  103. error = null;
  104. while (true)
  105. {
  106. var c = default(TSource);
  107. try
  108. {
  109. if (!e.MoveNext())
  110. break;
  111. c = e.Current;
  112. }
  113. catch (Exception ex)
  114. {
  115. error = ex;
  116. break;
  117. }
  118. yield return c;
  119. }
  120. if (error == null)
  121. break;
  122. }
  123. if (error != null)
  124. throw error;
  125. }
  126. }
  127. }