2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.io.PrintStream;
22 import java.io.PrintWriter;
23 import java.util.ArrayList;
24 import java.util.Collections;
25 import java.util.List;
28 * Wraps multiple exceptions.
30 * Allows multiple exceptions to be thrown as a single exception.
32 @SuppressWarnings("serial")
33 public class MultiException extends Exception
35 private List<Throwable> nested;
37 /* ------------------------------------------------------------ */
38 public MultiException()
40 super("Multiple exceptions");
43 /* ------------------------------------------------------------ */
44 public void add(Throwable e)
48 nested = new ArrayList<>();
51 if (e instanceof MultiException)
53 MultiException me = (MultiException)e;
54 nested.addAll(me.nested);
60 /* ------------------------------------------------------------ */
63 return (nested ==null)?0:nested.size();
66 /* ------------------------------------------------------------ */
67 public List<Throwable> getThrowables()
70 return Collections.emptyList();
75 /* ------------------------------------------------------------ */
76 public Throwable getThrowable(int i)
81 /* ------------------------------------------------------------ */
82 /** Throw a multiexception.
83 * If this multi exception is empty then no action is taken. If it
84 * contains a single exception that is thrown, otherwise the this
85 * multi exception is thrown.
86 * @exception Exception
88 public void ifExceptionThrow()
94 switch (nested.size())
99 Throwable th=nested.get(0);
100 if (th instanceof Error)
102 if (th instanceof Exception)
109 /* ------------------------------------------------------------ */
110 /** Throw a Runtime exception.
111 * If this multi exception is empty then no action is taken. If it
112 * contains a single error or runtime exception that is thrown, otherwise the this
113 * multi exception is thrown, wrapped in a runtime exception.
114 * @exception Error If this exception contains exactly 1 {@link Error}
115 * @exception RuntimeException If this exception contains 1 {@link Throwable} but it is not an error,
116 * or it contains more than 1 {@link Throwable} of any type.
118 public void ifExceptionThrowRuntime()
124 switch (nested.size())
129 Throwable th=nested.get(0);
130 if (th instanceof Error)
132 else if (th instanceof RuntimeException)
133 throw (RuntimeException)th;
135 throw new RuntimeException(th);
137 throw new RuntimeException(this);
141 /* ------------------------------------------------------------ */
142 /** Throw a multiexception.
143 * If this multi exception is empty then no action is taken. If it
144 * contains a any exceptions then this
145 * multi exception is thrown.
147 public void ifExceptionThrowMulti()
148 throws MultiException
157 /* ------------------------------------------------------------ */
159 public String toString()
161 StringBuilder str = new StringBuilder();
162 str.append(MultiException.class.getSimpleName());
163 if((nested == null) || (nested.size()<=0)) {
168 return str.toString();
171 /* ------------------------------------------------------------ */
173 public void printStackTrace()
175 super.printStackTrace();
177 for(Throwable t: nested) {
184 /* ------------------------------------------------------------------------------- */
186 * @see java.lang.Throwable#printStackTrace(java.io.PrintStream)
189 public void printStackTrace(PrintStream out)
191 super.printStackTrace(out);
193 for(Throwable t: nested) {
194 t.printStackTrace(out);
199 /* ------------------------------------------------------------------------------- */
201 * @see java.lang.Throwable#printStackTrace(java.io.PrintWriter)
204 public void printStackTrace(PrintWriter out)
206 super.printStackTrace(out);
208 for(Throwable t: nested) {
209 t.printStackTrace(out);