AggregateException.cs
Go to the documentation of this file.
1 // AggregateException.cs
2 //
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 //
24 
25 #if NET_4_0
26 using System;
27 using System.Collections.ObjectModel;
29 using System.Runtime.Serialization;
30 
31 namespace System
32 {
33 
34  [System.SerializableAttribute]
35  [System.Diagnostics.DebuggerDisplay ("Count = {InnerExceptions.Count}")]
36  public class AggregateException : Exception
37  {
38  List<Exception> innerExceptions = new List<Exception> ();
39  const string defaultMessage = "One or more errors occured";
40 
41  public AggregateException () : base (defaultMessage)
42  {
43  }
44 
45  public AggregateException (string message): base (message)
46  {
47  }
48 
49  public AggregateException (string message, Exception innerException): base (message, innerException)
50  {
51  if (innerException == null)
52  throw new ArgumentNullException ("innerException");
53  innerExceptions.Add (innerException);
54  }
55 
56  protected AggregateException (SerializationInfo info, StreamingContext context)
57  : base (info, context)
58  {
59  }
60 
61  public AggregateException (params Exception[] innerExceptions)
62  : this (string.Empty, innerExceptions)
63  {
64  }
65 
66  public AggregateException (string message, params Exception[] innerExceptions)
67  : base (message, innerExceptions == null || innerExceptions.Length == 0 ? null : innerExceptions[0])
68  {
69  if (innerExceptions == null)
70  throw new ArgumentNullException ("innerExceptions");
71  foreach (var exception in innerExceptions)
72  if (exception == null)
73  throw new ArgumentException ("One of the inner exception is null", "innerExceptions");
74 
75  this.innerExceptions.AddRange (innerExceptions);
76  }
77 
78  public AggregateException (IEnumerable<Exception> innerExceptions)
79  : this (defaultMessage, innerExceptions)
80  {
81  }
82 
83  public AggregateException (string message, IEnumerable<Exception> innerExceptions)
84  : this (message, new List<Exception> (innerExceptions).ToArray ())
85  {
86  }
87 
89  {
90  List<Exception> inner = new List<Exception> ();
91 
92  foreach (Exception e in innerExceptions) {
94  if (aggEx != null) {
95  inner.AddRange (aggEx.Flatten ().InnerExceptions);
96  } else {
97  inner.Add (e);
98  }
99  }
100 
101  return new AggregateException (inner);
102  }
103 
104  public void Handle (Func<Exception, bool> predicate)
105  {
106  List<Exception> failed = new List<Exception> ();
107  foreach (var e in innerExceptions) {
108  // Spicy Pixel: Mono is wrong here.
109  // Bug filed: https://bugzilla.xamarin.com/show_bug.cgi?id=11867
110  //
111  // This is what MSDN says:
112  //
113  // http://msdn.microsoft.com/en-us/library/system.aggregateexception.handle.aspx
114  // If any invocations of the predicate throws an exception, it will
115  // halt the processing of any more exceptions and immediately propagate
116  // the thrown exception as-is.
117  //try {
118  if (!predicate (e))
119  failed.Add (e);
120  //} catch {
121  // throw new AggregateException (failed);
122  //}
123  }
124  if (failed.Count > 0)
125  throw new AggregateException (failed);
126  }
127 
128  public ReadOnlyCollection<Exception> InnerExceptions {
129  get {
130  return innerExceptions.AsReadOnly ();
131  }
132  }
133 
134  internal void AddChildException (AggregateException childEx)
135  {
136  if (innerExceptions == null)
137  innerExceptions = new List<Exception> ();
138  if (childEx == null)
139  return;
140 
141  innerExceptions.Add (childEx);
142  }
143 
144  public override string ToString ()
145  {
146  System.Text.StringBuilder finalMessage = new System.Text.StringBuilder (base.ToString ());
147 
148  int currentIndex = -1;
149  foreach (Exception e in innerExceptions) {
150  finalMessage.Append (Environment.NewLine);
151  finalMessage.Append (" --> (Inner exception ");
152  finalMessage.Append (++currentIndex);
153  finalMessage.Append (") ");
154  finalMessage.Append (e.ToString ());
155  finalMessage.Append (Environment.NewLine);
156  }
157  return finalMessage.ToString ();
158  }
159 
160  public override void GetObjectData (SerializationInfo info, StreamingContext context)
161  {
162  if (info == null) {
163  throw new ArgumentNullException("info");
164  }
165  base.GetObjectData(info, context);
166  info.AddValue ("InnerExceptions", innerExceptions.ToArray(), typeof (Exception[]));
167  }
168 
169  public override Exception GetBaseException ()
170  {
171  if (innerExceptions == null || innerExceptions.Count == 0)
172  return this;
173  return innerExceptions[0].GetBaseException ();
174  }
175  }
176 }
177 #endif
AggregateException(params Exception[] innerExceptions)
void Handle(Func< Exception, bool > predicate)
AggregateException(string message, IEnumerable< Exception > innerExceptions)
override Exception GetBaseException()
AggregateException(string message, params Exception[] innerExceptions)
override void GetObjectData(SerializationInfo info, StreamingContext context)
AggregateException(string message, Exception innerException)
ReadOnlyCollection< Exception > InnerExceptions
AggregateException Flatten()
AggregateException(string message)
AggregateException(IEnumerable< Exception > innerExceptions)
AggregateException(SerializationInfo info, StreamingContext context)