1
0

EnumerableEx.Exceptions.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace System.Linq
  8. {
  9. public static partial class EnumerableEx
  10. {
  11. /// <summary>
  12. /// Creates a sequence that corresponds to the source sequence, concatenating it with the sequence resulting from calling an exception handler function in case of an error.
  13. /// </summary>
  14. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  15. /// <typeparam name="TException">Exception type to catch.</typeparam>
  16. /// <param name="source">Source sequence.</param>
  17. /// <param name="handler">Handler to invoke when an exception of the specified type occurs.</param>
  18. /// <returns>Source sequence, concatenated with an exception handler result sequence in case of an error.</returns>
  19. public static IEnumerable<TSource> Catch<TSource, TException>(this IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler)
  20. where TException : Exception
  21. {
  22. if (source == null)
  23. throw new ArgumentNullException(nameof(source));
  24. if (handler == null)
  25. throw new ArgumentNullException(nameof(handler));
  26. return source.Catch_(handler);
  27. }
  28. private static IEnumerable<TSource> Catch_<TSource, TException>(this IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler)
  29. where TException : Exception
  30. {
  31. var err = default(IEnumerable<TSource>);
  32. using (var e = source.GetEnumerator())
  33. {
  34. while (true)
  35. {
  36. var c = default(TSource);
  37. try
  38. {
  39. if (!e.MoveNext())
  40. break;
  41. c = e.Current;
  42. }
  43. catch (TException ex)
  44. {
  45. err = handler(ex);
  46. break;
  47. }
  48. yield return c;
  49. }
  50. }
  51. if (err != null)
  52. {
  53. foreach (var item in err)
  54. yield return item;
  55. }
  56. }
  57. /// <summary>
  58. /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
  59. /// </summary>
  60. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  61. /// <param name="sources">Source sequences.</param>
  62. /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
  63. public static IEnumerable<TSource> Catch<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
  64. {
  65. if (sources == null)
  66. throw new ArgumentNullException(nameof(sources));
  67. return sources.Catch_();
  68. }
  69. /// <summary>
  70. /// Creates a sequence by concatenating source sequences until a source sequence completes successfully.
  71. /// </summary>
  72. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  73. /// <param name="sources">Source sequences.</param>
  74. /// <returns>Sequence that continues to concatenate source sequences while errors occur.</returns>
  75. public static IEnumerable<TSource> Catch<TSource>(params IEnumerable<TSource>[] sources)
  76. {
  77. if (sources == null)
  78. throw new ArgumentNullException(nameof(sources));
  79. return sources.Catch_();
  80. }
  81. /// <summary>
  82. /// Creates a sequence that returns the elements of the first sequence, switching to the second in case of an error.
  83. /// </summary>
  84. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  85. /// <param name="first">First sequence.</param>
  86. /// <param name="second">Second sequence, concatenated to the result in case the first sequence completes exceptionally.</param>
  87. /// <returns>The first sequence, followed by the second sequence in case an error is produced.</returns>
  88. public static IEnumerable<TSource> Catch<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
  89. {
  90. if (first == null)
  91. throw new ArgumentNullException(nameof(first));
  92. if (second == null)
  93. throw new ArgumentNullException(nameof(second));
  94. return new[] { first, second }.Catch_();
  95. }
  96. private static IEnumerable<TSource> Catch_<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
  97. {
  98. var error = default(Exception);
  99. foreach (var source in sources)
  100. {
  101. using (var e = source.GetEnumerator())
  102. {
  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. }
  124. if (error != null)
  125. throw error;
  126. }
  127. /// <summary>
  128. /// Creates a sequence whose termination or disposal of an enumerator causes a finally action to be executed.
  129. /// </summary>
  130. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  131. /// <param name="source">Source sequence.</param>
  132. /// <param name="finallyAction">Action to run upon termination of the sequence, or when an enumerator is disposed.</param>
  133. /// <returns>Source sequence with guarantees on the invocation of the finally action.</returns>
  134. public static IEnumerable<TSource> Finally<TSource>(this IEnumerable<TSource> source, Action finallyAction)
  135. {
  136. if (source == null)
  137. throw new ArgumentNullException(nameof(source));
  138. if (finallyAction == null)
  139. throw new ArgumentNullException(nameof(finallyAction));
  140. return source.Finally_(finallyAction);
  141. }
  142. private static IEnumerable<TSource> Finally_<TSource>(this IEnumerable<TSource> source, Action finallyAction)
  143. {
  144. try
  145. {
  146. foreach (var item in source)
  147. yield return item;
  148. }
  149. finally
  150. {
  151. finallyAction();
  152. }
  153. }
  154. /// <summary>
  155. /// Creates a sequence that concatenates both given sequences, regardless of whether an error occurs.
  156. /// </summary>
  157. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  158. /// <param name="first">First sequence.</param>
  159. /// <param name="second">Second sequence.</param>
  160. /// <returns>Sequence concatenating the elements of both sequences, ignoring errors.</returns>
  161. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
  162. {
  163. if (first == null)
  164. throw new ArgumentNullException(nameof(first));
  165. if (second == null)
  166. throw new ArgumentNullException(nameof(second));
  167. return OnErrorResumeNext_(new[] { first, second });
  168. }
  169. /// <summary>
  170. /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the sequences.
  171. /// </summary>
  172. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  173. /// <param name="sources">Source sequences.</param>
  174. /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
  175. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(params IEnumerable<TSource>[] sources)
  176. {
  177. if (sources == null)
  178. throw new ArgumentNullException(nameof(sources));
  179. return OnErrorResumeNext_(sources);
  180. }
  181. /// <summary>
  182. /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the sequences.
  183. /// </summary>
  184. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  185. /// <param name="sources">Source sequences.</param>
  186. /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
  187. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
  188. {
  189. if (sources == null)
  190. throw new ArgumentNullException(nameof(sources));
  191. return OnErrorResumeNext_(sources);
  192. }
  193. private static IEnumerable<TSource> OnErrorResumeNext_<TSource>(IEnumerable<IEnumerable<TSource>> sources)
  194. {
  195. foreach (var source in sources)
  196. {
  197. using (var innerEnumerator = source.GetEnumerator())
  198. {
  199. while (true)
  200. {
  201. var value = default(TSource);
  202. try
  203. {
  204. if (!innerEnumerator.MoveNext())
  205. break;
  206. value = innerEnumerator.Current;
  207. }
  208. catch
  209. {
  210. break;
  211. }
  212. yield return value;
  213. }
  214. }
  215. }
  216. }
  217. /// <summary>
  218. /// Creates a sequence that retries enumerating the source sequence as long as an error occurs.
  219. /// </summary>
  220. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  221. /// <param name="source">Source sequence.</param>
  222. /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
  223. public static IEnumerable<TSource> Retry<TSource>(this IEnumerable<TSource> source)
  224. {
  225. if (source == null)
  226. throw new ArgumentNullException(nameof(source));
  227. return new[] { source }.Repeat().Catch();
  228. }
  229. /// <summary>
  230. /// Creates a sequence that retries enumerating the source sequence as long as an error occurs, with the specified maximum number of retries.
  231. /// </summary>
  232. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  233. /// <param name="source">Source sequence.</param>
  234. /// <param name="retryCount">Maximum number of retries.</param>
  235. /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
  236. public static IEnumerable<TSource> Retry<TSource>(this IEnumerable<TSource> source, int retryCount)
  237. {
  238. if (source == null)
  239. throw new ArgumentNullException(nameof(source));
  240. if (retryCount < 0)
  241. throw new ArgumentOutOfRangeException(nameof(retryCount));
  242. return new[] { source }.Repeat(retryCount).Catch();
  243. }
  244. }
  245. }