1616package rx .exceptions ;
1717
1818import java .util .HashSet ;
19+ import java .util .List ;
1920import java .util .Set ;
2021
22+ import rx .annotations .Experimental ;
23+
2124/**
2225 * @warn javadoc class description missing
2326 */
@@ -27,7 +30,9 @@ private Exceptions() {
2730 }
2831
2932 /**
30- * @warn javadoc missing
33+ * Convenience method to throw a {@code RuntimeException} and {@code Error} directly
34+ * or wrap any other exception type into a {@code RuntimeException}.
35+ * @param t the exception to throw directly or wrapped
3136 * @return because {@code propagate} itself throws an exception or error, this is a sort of phantom return
3237 * value; {@code propagate} does not actually return anything
3338 */
@@ -48,7 +53,6 @@ public static RuntimeException propagate(Throwable t) {
4853 throw new RuntimeException (t );
4954 }
5055 }
51-
5256 /**
5357 * Throws a particular {@code Throwable} only if it belongs to a set of "fatal" error varieties. These
5458 * varieties are as follows:
@@ -148,5 +152,30 @@ public static Throwable getFinalCause(Throwable e) {
148152 }
149153 return e ;
150154 }
151-
155+ /**
156+ * Throws a single or multiple exceptions contained in the collection, wrapping it into
157+ * {@code CompositeException} if necessary.
158+ * @param exceptions the collection of exceptions. If null or empty, no exception is thrown.
159+ * If the collection contains a single exception, that exception is either thrown as-is or wrapped into a
160+ * CompositeException. Multiple exceptions are wrapped into a CompositeException.
161+ */
162+ @ Experimental
163+ public static void throwIfAny (List <? extends Throwable > exceptions ) {
164+ if (exceptions != null && !exceptions .isEmpty ()) {
165+ if (exceptions .size () == 1 ) {
166+ Throwable t = exceptions .get (0 );
167+ // had to manually inline propagate because some tests attempt StackOverflowError
168+ // and can't handle it with the stack space remaining
169+ if (t instanceof RuntimeException ) {
170+ throw (RuntimeException ) t ;
171+ } else if (t instanceof Error ) {
172+ throw (Error ) t ;
173+ } else {
174+ throw new RuntimeException (t );
175+ }
176+ }
177+ throw new CompositeException (
178+ "Multiple exceptions" , exceptions );
179+ }
180+ }
152181}
0 commit comments