1
0

Catch.cs 6.0 KB

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