Skip to content

Commit 4b4bea2

Browse files
akarnokdakarnokd
akarnokd
authored and
akarnokd
committed
Added experimental annotation, using propagate.
1 parent a945b16 commit 4b4bea2

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

src/main/java/rx/exceptions/Exceptions.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
*/
1616
package rx.exceptions;
1717

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;
1923

2024
/**
2125
* @warn javadoc class description missing
@@ -26,11 +30,26 @@ private Exceptions() {
2630
}
2731

2832
/**
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
3036
* @return because {@code propagate} itself throws an exception or error, this is a sort of phantom return
3137
* value; {@code propagate} does not actually return anything
3238
*/
3339
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) {
3453
/*
3554
* The return type of RuntimeException is a trick for code to be like this:
3655
*
@@ -44,7 +63,7 @@ public static RuntimeException propagate(Throwable t) {
4463
} else if (t instanceof Error) {
4564
throw (Error) t;
4665
} else {
47-
throw new RuntimeException(t);
66+
throw new RuntimeException(message, t);
4867
}
4968
}
5069

@@ -156,19 +175,12 @@ public static Throwable getFinalCause(Throwable e) {
156175
* @param whileText the circumstance string to be appended to the thrown CompositeException, inserted after
157176
* the sentences "Exception" and "Multiple exceptions".
158177
*/
178+
@Experimental
159179
public static void throwIfAny(Collection<? extends Throwable> exceptions, String whileText) {
160180
if (exceptions != null && !exceptions.isEmpty()) {
161181
if (exceptions.size() == 1) {
162182
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);
172184
} else {
173185
throw new CompositeException(
174186
"Multiple exceptions" + whileText, exceptions);

0 commit comments

Comments
 (0)