2 // ========================================================================
3 // Copyright (c) 1995-2016 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.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
26 * Wraps multiple exceptions.
28 * Allows multiple exceptions to be thrown as a single exception.
30 @SuppressWarnings("serial")
31 public class MultiException extends Exception
33 private List<Throwable> nested;
35 /* ------------------------------------------------------------ */
36 public MultiException()
38 super("Multiple exceptions");
41 /* ------------------------------------------------------------ */
42 public void add(Throwable e)
45 throw new IllegalArgumentException();
50 nested = new ArrayList<>();
55 if (e instanceof MultiException)
57 MultiException me = (MultiException)e;
58 nested.addAll(me.nested);
64 /* ------------------------------------------------------------ */
67 return (nested ==null)?0:nested.size();
70 /* ------------------------------------------------------------ */
71 public List<Throwable> getThrowables()
74 return Collections.emptyList();
78 /* ------------------------------------------------------------ */
79 public Throwable getThrowable(int i)
84 /* ------------------------------------------------------------ */
85 /** Throw a multiexception.
86 * If this multi exception is empty then no action is taken. If it
87 * contains a single exception that is thrown, otherwise the this
88 * multi exception is thrown.
89 * @exception Exception
91 public void ifExceptionThrow()
97 switch (nested.size())
102 Throwable th=nested.get(0);
103 if (th instanceof Error)
105 if (th instanceof Exception)
112 /* ------------------------------------------------------------ */
113 /** Throw a Runtime exception.
114 * If this multi exception is empty then no action is taken. If it
115 * contains a single error or runtime exception that is thrown, otherwise the this
116 * multi exception is thrown, wrapped in a runtime exception.
117 * @exception Error If this exception contains exactly 1 {@link Error}
118 * @exception RuntimeException If this exception contains 1 {@link Throwable} but it is not an error,
119 * or it contains more than 1 {@link Throwable} of any type.
121 public void ifExceptionThrowRuntime()
127 switch (nested.size())
132 Throwable th=nested.get(0);
133 if (th instanceof Error)
135 else if (th instanceof RuntimeException)
136 throw (RuntimeException)th;
138 throw new RuntimeException(th);
140 throw new RuntimeException(this);
144 /* ------------------------------------------------------------ */
145 /** Throw a multiexception.
146 * If this multi exception is empty then no action is taken. If it
147 * contains a any exceptions then this
148 * multi exception is thrown.
150 public void ifExceptionThrowMulti()
151 throws MultiException
160 /* ------------------------------------------------------------ */
162 public String toString()
164 StringBuilder str = new StringBuilder();
165 str.append(MultiException.class.getSimpleName());
166 if((nested == null) || (nested.size()<=0)) {
171 return str.toString();