2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / corlib / System / AggregateException.cs
blob0e02b536870459146ff7a99a4648b26ec3bb701b
1 #if NET_4_0 || BOOTSTRAP_NET_4_0
2 // AggregateException.cs
3 //
4 // Copyright (c) 2008 Jérémie "Garuma" Laval
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a copy
7 // of this software and associated documentation files (the "Software"), to deal
8 // in the Software without restriction, including without limitation the rights
9 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 // copies of the Software, and to permit persons to whom the Software is
11 // furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 // THE SOFTWARE.
26 using System;
27 using System.Collections.ObjectModel;
28 using System.Collections.Generic;
29 using System.Runtime.Serialization;
31 namespace System
33 [System.SerializableAttribute]
34 public class AggregateException : Exception
36 List<Exception> innerExceptions;
38 public AggregateException (): base()
42 public AggregateException (string message): base (message, null)
46 public AggregateException (string message, Exception e): base (message, e)
50 protected AggregateException (SerializationInfo info, StreamingContext ctx)
51 : base (info, ctx)
55 public AggregateException (params Exception[] innerExceptions)
56 : this (string.Empty, innerExceptions)
60 public AggregateException (string message, params Exception[] innerExceptions)
61 : this (message, (IEnumerable<Exception>)innerExceptions)
65 public AggregateException (IEnumerable<Exception> innerExceptions)
66 : this (string.Empty, innerExceptions)
70 public AggregateException (string message, IEnumerable<Exception> inner)
71 : base(GetFormattedMessage(message, inner))
73 this.innerExceptions = new List<Exception> (inner);
76 public AggregateException Flatten ()
78 List<Exception> inner = new List<Exception> ();
80 foreach (Exception e in innerExceptions) {
81 AggregateException aggEx = e as AggregateException;
82 if (aggEx != null) {
83 inner.AddRange (aggEx.Flatten ().InnerExceptions);
84 } else {
85 inner.Add (e);
89 return new AggregateException (inner);
92 public void Handle (Func<Exception, bool> handler)
94 List<Exception> failed = new List<Exception> ();
95 foreach (var e in innerExceptions) {
96 try {
97 if (!handler (e))
98 failed.Add (e);
99 } catch {
100 throw new AggregateException (failed);
103 if (failed.Count > 0)
104 throw new AggregateException (failed);
107 public ReadOnlyCollection<Exception> InnerExceptions {
108 get {
109 return innerExceptions.AsReadOnly ();
113 public override string ToString ()
115 return this.Message;
118 const string baseMessage = "Exception(s) occurred : {0}.";
119 static string GetFormattedMessage (string customMessage, IEnumerable<Exception> inner)
121 System.Text.StringBuilder finalMessage
122 = new System.Text.StringBuilder (string.Format (baseMessage, customMessage));
123 foreach (Exception e in inner) {
124 finalMessage.Append (Environment.NewLine);
125 finalMessage.Append ("[ ");
126 finalMessage.Append (e.ToString ());
127 finalMessage.Append (" ]");
128 finalMessage.Append (Environment.NewLine);
130 return finalMessage.ToString ();
134 #endif