-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added common Exceptions.throwIfAny to throw a collection of exceptions #2601
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,9 +15,12 @@ | |
*/ | ||
package rx.exceptions; | ||
|
||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import rx.annotations.Experimental; | ||
|
||
/** | ||
* @warn javadoc class description missing | ||
*/ | ||
|
@@ -27,11 +30,26 @@ private Exceptions() { | |
} | ||
|
||
/** | ||
* @warn javadoc missing | ||
* Convenience method to throw a {@code RuntimeException} and {@code Error} directly | ||
* or wrap any other exception type into a {@code RuntimeException}. | ||
* @param t the exception to throw directly or wrapped | ||
* @return because {@code propagate} itself throws an exception or error, this is a sort of phantom return | ||
* value; {@code propagate} does not actually return anything | ||
*/ | ||
public static RuntimeException propagate(Throwable t) { | ||
return propagate(t, null); | ||
} | ||
/** | ||
* Convenience method to throw a {@code RuntimeException} and {@code Error} directly | ||
* or wrap any other exception type into a {@code RuntimeException} with an optional custom message. | ||
* @param t the exception to throw directly or wrapped | ||
* @param message the optional custom message to set up the RuntimeException thrown | ||
* in case {@code t} is a checked exception. | ||
* @return because {@code propagate} itself throws an exception or error, this is a sort of phantom return | ||
* value; {@code propagate} does not actually return anything | ||
*/ | ||
@Experimental | ||
public static RuntimeException propagate(Throwable t, String message) { | ||
/* | ||
* The return type of RuntimeException is a trick for code to be like this: | ||
* | ||
|
@@ -45,7 +63,7 @@ public static RuntimeException propagate(Throwable t) { | |
} else if (t instanceof Error) { | ||
throw (Error) t; | ||
} else { | ||
throw new RuntimeException(t); | ||
throw new RuntimeException(message, t); | ||
} | ||
} | ||
|
||
|
@@ -148,5 +166,25 @@ public static Throwable getFinalCause(Throwable e) { | |
} | ||
return e; | ||
} | ||
|
||
/** | ||
* Throws a single or multiple exceptions contained in the collection, wrapping it into | ||
* {@code CompositeException} if necessary. | ||
* @param exceptions the collection of exceptions. If null or empty, no exception is thrown. | ||
* If the collection contains a single exception, that exception is either thrown as-is or wrapped into a | ||
* CompositeException. Multiple exceptions are wrapped into a CompositeException. | ||
* @param whileText the circumstance string to be appended to the thrown CompositeException, inserted after | ||
* the sentences "Exception" and "Multiple exceptions". | ||
*/ | ||
@Experimental | ||
public static void throwIfAny(Collection<? extends Throwable> exceptions, String whileText) { | ||
if (exceptions != null && !exceptions.isEmpty()) { | ||
if (exceptions.size() == 1) { | ||
Throwable t = exceptions.iterator().next(); | ||
throw propagate(t, "Exception" + whileText); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
} else { | ||
throw new CompositeException( | ||
"Multiple exceptions" + whileText, exceptions); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be easier to read:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because the original places distinguished between single and multiple expections in the message of CompositeException. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As per the discussion at #2602 ... this is an example where @akarnokd and I have slightly different styles, but it's okay. I always have Sometimes when @akarnokd and I end up modifying each others code we'll see some lines get modified to personal style if we touch them, but we don't go around explicitly trying to change code just to match a personal preference ... if for no other reason than to not clutter changelogs and diffs. |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have return values on these when they end up just throwing? I just noticed that. We can't change it, but it's very odd.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is explained by the javadoc.