ExceptionList.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.ExceptionServices;
  4. using System.Text;
  5. namespace FF8
  6. {
  7. public sealed class ExceptionList : IDisposable
  8. {
  9. private readonly List<Exception> _exceptions = new List<Exception>();
  10. public void Add(Exception ex)
  11. {
  12. _exceptions.Add(ex);
  13. }
  14. public void Clear()
  15. {
  16. _exceptions.Clear();
  17. }
  18. public void Dispose()
  19. {
  20. switch (_exceptions.Count)
  21. {
  22. case 0:
  23. {
  24. return;
  25. }
  26. case 1:
  27. {
  28. ExceptionDispatchInfo.Capture(_exceptions[0]).Throw();
  29. return;
  30. }
  31. default:
  32. {
  33. StringBuilder sb = new StringBuilder();
  34. foreach (var ex in _exceptions)
  35. sb.AppendLine(ex.Message);
  36. throw new AggregateException(sb.ToString(), _exceptions);
  37. }
  38. }
  39. }
  40. }
  41. }