25 #if NET_4_0 || UNITY_5_3_OR_NEWER 34 [
System.SerializableAttribute]
35 [
System.Diagnostics.DebuggerDisplay (
"Count = {InnerExceptions.Count}")]
38 List<Exception> innerExceptions =
new List<Exception> ();
39 const string defaultMessage =
"One or more errors occured";
49 public AggregateException (
string message, Exception innerException): base (message, innerException)
51 if (innerException ==
null)
52 throw new ArgumentNullException (
"innerException");
53 innerExceptions.Add (innerException);
57 : base (info, context)
62 : this (string.Empty, innerExceptions)
67 : base (message, innerExceptions == null || innerExceptions.Length == 0 ? null : innerExceptions[0])
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");
75 this.innerExceptions.AddRange (innerExceptions);
79 : this (defaultMessage, innerExceptions)
84 : this (message, new List<Exception> (innerExceptions).ToArray ())
90 List<Exception> inner =
new List<Exception> ();
92 foreach (Exception e
in innerExceptions) {
104 public void Handle (Func<Exception, bool> predicate)
106 List<Exception> failed =
new List<Exception> ();
107 foreach (var e
in innerExceptions) {
124 if (failed.Count > 0)
130 return innerExceptions.AsReadOnly ();
136 if (innerExceptions ==
null)
137 innerExceptions =
new List<Exception> ();
141 innerExceptions.Add (childEx);
146 System.Text.StringBuilder finalMessage =
new System.Text.StringBuilder (base.ToString ());
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);
157 return finalMessage.ToString ();
160 public override void GetObjectData (SerializationInfo info, StreamingContext context)
163 throw new ArgumentNullException(
"info");
165 base.GetObjectData(info, context);
166 info.AddValue (
"InnerExceptions", innerExceptions.ToArray(), typeof (Exception[]));
171 if (innerExceptions ==
null || innerExceptions.Count == 0)
173 return innerExceptions[0].GetBaseException ();
AggregateException(params Exception[] innerExceptions)
void Handle(Func< Exception, bool > predicate)
AggregateException(string message, IEnumerable< Exception > innerExceptions)
override Exception GetBaseException()
override void GetObjectData(SerializationInfo info, StreamingContext context)
AggregateException(string message, Exception innerException)
override string ToString()
ReadOnlyCollection< Exception > InnerExceptions
AggregateException Flatten()
AggregateException(string message)
AggregateException(IEnumerable< Exception > innerExceptions)
AggregateException(SerializationInfo info, StreamingContext context)