15
15
*/
16
16
package rx .exceptions ;
17
17
18
- import java .util .*;
18
+ import java .util .Collection ;
19
+ import java .util .HashSet ;
20
+ import java .util .Set ;
21
+
22
+ import rx .annotations .Experimental ;
19
23
20
24
/**
21
25
* @warn javadoc class description missing
@@ -26,11 +30,26 @@ private Exceptions() {
26
30
}
27
31
28
32
/**
29
- * @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
30
36
* @return because {@code propagate} itself throws an exception or error, this is a sort of phantom return
31
37
* value; {@code propagate} does not actually return anything
32
38
*/
33
39
public static RuntimeException propagate (Throwable t ) {
40
+ return propagate (t , null );
41
+ }
42
+ /**
43
+ * Convenience method to throw a {@code RuntimeException} and {@code Error} directly
44
+ * or wrap any other exception type into a {@code RuntimeException} with an optional custom message.
45
+ * @param t the exception to throw directly or wrapped
46
+ * @param message the optional custom message to set up the RuntimeException thrown
47
+ * in case {@code t} is a checked exception.
48
+ * @return because {@code propagate} itself throws an exception or error, this is a sort of phantom return
49
+ * value; {@code propagate} does not actually return anything
50
+ */
51
+ @ Experimental
52
+ public static RuntimeException propagate (Throwable t , String message ) {
34
53
/*
35
54
* The return type of RuntimeException is a trick for code to be like this:
36
55
*
@@ -44,7 +63,7 @@ public static RuntimeException propagate(Throwable t) {
44
63
} else if (t instanceof Error ) {
45
64
throw (Error ) t ;
46
65
} else {
47
- throw new RuntimeException (t );
66
+ throw new RuntimeException (message , t );
48
67
}
49
68
}
50
69
@@ -156,19 +175,12 @@ public static Throwable getFinalCause(Throwable e) {
156
175
* @param whileText the circumstance string to be appended to the thrown CompositeException, inserted after
157
176
* the sentences "Exception" and "Multiple exceptions".
158
177
*/
178
+ @ Experimental
159
179
public static void throwIfAny (Collection <? extends Throwable > exceptions , String whileText ) {
160
180
if (exceptions != null && !exceptions .isEmpty ()) {
161
181
if (exceptions .size () == 1 ) {
162
182
Throwable t = exceptions .iterator ().next ();
163
- if (t instanceof RuntimeException ) {
164
- throw (RuntimeException ) t ;
165
- } else
166
- if (t instanceof Error ) {
167
- throw (Error ) t ;
168
- } else {
169
- throw new CompositeException (
170
- "Exception" + whileText , exceptions );
171
- }
183
+ throw propagate (t , "Exception" + whileText );
172
184
} else {
173
185
throw new CompositeException (
174
186
"Multiple exceptions" + whileText , exceptions );
0 commit comments