EnumerableEx.Exceptions.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  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 calling an exception handler function in case of an error.
  11. /// </summary>
  12. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  13. /// <typeparam name="TException">Exception type to catch.</typeparam>
  14. /// <param name="source">Source sequence.</param>
  15. /// <param name="handler">Handler to invoke when an exception of the specified type occurs.</param>
  16. /// <returns>Source sequence, concatenated with an exception handler result sequence in case of an error.</returns>
  17. public static IEnumerable<TSource> Catch<TSource, TException>(this IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler)
  18. where TException : Exception
  19. {
  20. if (source == null)
  21. throw new ArgumentNullException("source");
  22. if (handler == null)
  23. throw new ArgumentNullException("handler");
  24. return source.Catch_(handler);
  25. }
  26. private static IEnumerable<TSource> Catch_<TSource, TException>(this IEnumerable<TSource> source, Func<TException, IEnumerable<TSource>> handler)
  27. where TException : Exception
  28. {
  29. var err = default(IEnumerable<TSource>);
  30. using (var e = source.GetEnumerator())
  31. {
  32. while (true)
  33. {
  34. var b = default(bool);
  35. var c = default(TSource);
  36. try
  37. {
  38. b = e.MoveNext();
  39. c = e.Current;
  40. }
  41. catch (TException ex)
  42. {
  43. err = handler(ex);
  44. break;
  45. }
  46. if (!b)
  47. break;
  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("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("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("first");
  92. if (second == null)
  93. throw new ArgumentNullException("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 b = default(bool);
  107. var c = default(TSource);
  108. try
  109. {
  110. b = e.MoveNext();
  111. c = e.Current;
  112. }
  113. catch (Exception ex)
  114. {
  115. error = ex;
  116. break;
  117. }
  118. if (!b)
  119. break;
  120. yield return c;
  121. }
  122. if (error == null)
  123. break;
  124. }
  125. }
  126. if (error != null)
  127. throw error;
  128. }
  129. /// <summary>
  130. /// Creates a sequence whose termination or disposal of an enumerator causes a finally action to be executed.
  131. /// </summary>
  132. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  133. /// <param name="source">Source sequence.</param>
  134. /// <param name="finallyAction">Action to run upon termination of the sequence, or when an enumerator is disposed.</param>
  135. /// <returns>Source sequence with guarantees on the invocation of the finally action.</returns>
  136. public static IEnumerable<TSource> Finally<TSource>(this IEnumerable<TSource> source, Action finallyAction)
  137. {
  138. if (source == null)
  139. throw new ArgumentNullException("source");
  140. if (finallyAction == null)
  141. throw new ArgumentNullException("finallyAction");
  142. return source.Finally_(finallyAction);
  143. }
  144. private static IEnumerable<TSource> Finally_<TSource>(this IEnumerable<TSource> source, Action finallyAction)
  145. {
  146. try
  147. {
  148. foreach (var item in source)
  149. yield return item;
  150. }
  151. finally
  152. {
  153. finallyAction();
  154. }
  155. }
  156. /// <summary>
  157. /// Creates a sequence that concatenates both given sequences, regardless of whether an error occurs.
  158. /// </summary>
  159. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  160. /// <param name="first">First sequence.</param>
  161. /// <param name="second">Second sequence.</param>
  162. /// <returns>Sequence concatenating the elements of both sequences, ignoring errors.</returns>
  163. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
  164. {
  165. if (first == null)
  166. throw new ArgumentNullException("first");
  167. if (second == null)
  168. throw new ArgumentNullException("second");
  169. return OnErrorResumeNext_(new[] { first, second });
  170. }
  171. /// <summary>
  172. /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the sequences.
  173. /// </summary>
  174. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  175. /// <param name="sources">Source sequences.</param>
  176. /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
  177. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(params IEnumerable<TSource>[] sources)
  178. {
  179. if (sources == null)
  180. throw new ArgumentNullException("sources");
  181. return OnErrorResumeNext_(sources);
  182. }
  183. /// <summary>
  184. /// Creates a sequence that concatenates the given sequences, regardless of whether an error occurs in any of the sequences.
  185. /// </summary>
  186. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  187. /// <param name="sources">Source sequences.</param>
  188. /// <returns>Sequence concatenating the elements of the given sequences, ignoring errors.</returns>
  189. public static IEnumerable<TSource> OnErrorResumeNext<TSource>(this IEnumerable<IEnumerable<TSource>> sources)
  190. {
  191. if (sources == null)
  192. throw new ArgumentNullException("sources");
  193. return OnErrorResumeNext_(sources);
  194. }
  195. private static IEnumerable<TSource> OnErrorResumeNext_<TSource>(IEnumerable<IEnumerable<TSource>> sources)
  196. {
  197. foreach (var source in sources)
  198. {
  199. using (var innerEnumerator = source.GetEnumerator())
  200. {
  201. while (true)
  202. {
  203. var value = default(TSource);
  204. try
  205. {
  206. if (!innerEnumerator.MoveNext())
  207. break;
  208. value = innerEnumerator.Current;
  209. }
  210. catch
  211. {
  212. break;
  213. }
  214. yield return value;
  215. }
  216. }
  217. }
  218. }
  219. /// <summary>
  220. /// Creates a sequence that retries enumerating the source sequence as long as an error occurs.
  221. /// </summary>
  222. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  223. /// <param name="source">Source sequence.</param>
  224. /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
  225. public static IEnumerable<TSource> Retry<TSource>(this IEnumerable<TSource> source)
  226. {
  227. if (source == null)
  228. throw new ArgumentNullException("source");
  229. return new[] { source }.Repeat().Catch();
  230. }
  231. /// <summary>
  232. /// Creates a sequence that retries enumerating the source sequence as long as an error occurs, with the specified maximum number of retries.
  233. /// </summary>
  234. /// <typeparam name="TSource">Source sequence element type.</typeparam>
  235. /// <param name="source">Source sequence.</param>
  236. /// <param name="retryCount">Maximum number of retries.</param>
  237. /// <returns>Sequence concatenating the results of the source sequence as long as an error occurs.</returns>
  238. public static IEnumerable<TSource> Retry<TSource>(this IEnumerable<TSource> source, int retryCount)
  239. {
  240. if (source == null)
  241. throw new ArgumentNullException("source");
  242. if (retryCount < 0)
  243. throw new ArgumentOutOfRangeException("retryCount");
  244. return new[] { source }.Repeat(retryCount).Catch();
  245. }
  246. }
  247. }