com.google.common.testing
Class ClusterException
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
com.google.common.testing.ClusterException
- All Implemented Interfaces:
- java.io.Serializable
final class ClusterException
- extends java.lang.RuntimeException
An ClusterException
is data structure that allows for some code to
"throw multiple exceptions", or something close to it. The prototypical code
that calls for this class is presented below:
void runManyThings(List<ThingToRun> thingsToRun) {
for (ThingToRun thingToRun : thingsToRun) {
thingToRun.run(); // <-- say this may throw an exception, but you want to
// always run all thingsToRun
}
}
This is what the code would become:
void runManyThings(List<ThingToRun> thingsToRun) {
List<Exception> exceptions = Lists.newArrayList();
for (ThingToRun thingToRun : thingsToRun) {
try {
thingToRun.run();
} catch (Exception e) {
exceptions.add(e);
}
}
if (exceptions.size() > 0) {
throw ExceptionCluster.create(exceptions);
}
}
See semantic details at create(Collection)
.
- Author:
- Luiz-Otavio Zorzella
Field Summary |
java.util.Collection<? extends java.lang.Throwable> |
exceptions
|
Constructor Summary |
private |
ClusterException(java.util.Collection<? extends java.lang.Throwable> exceptions)
|
Method Summary |
static java.lang.RuntimeException |
create(java.util.Collection<? extends java.lang.Throwable> exceptions)
Given a collection of exceptions, returns a RuntimeException , with
the following rules:
If exceptions has a single exception and that exception is a
RuntimeException , return it
If exceptions has a single exceptions and that exceptions is
not a RuntimeException , return a simple
RuntimeException that wraps it
Otherwise, return an instance of ClusterException that wraps
the first exception in the exceptions collection. |
static java.lang.RuntimeException |
create(java.lang.Throwable... exceptions)
|
Methods inherited from class java.lang.Throwable |
fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
exceptions
public final java.util.Collection<? extends java.lang.Throwable> exceptions
ClusterException
private ClusterException(java.util.Collection<? extends java.lang.Throwable> exceptions)
create
public static java.lang.RuntimeException create(java.lang.Throwable... exceptions)
- See Also:
create(Collection)
create
public static java.lang.RuntimeException create(java.util.Collection<? extends java.lang.Throwable> exceptions)
- Given a collection of exceptions, returns a
RuntimeException
, with
the following rules:
- If
exceptions
has a single exception and that exception is a
RuntimeException
, return it
- If
exceptions
has a single exceptions and that exceptions is
not a RuntimeException
, return a simple
RuntimeException
that wraps it
- Otherwise, return an instance of
ClusterException
that wraps
the first exception in the exceptions
collection.
Though this method takes any Collection
, it often makes most
sense to pass a List
or some other collection that
preserves the order in which the exceptions got added.
- Throws:
java.lang.NullPointerException
- if exceptions
is null
java.lang.IllegalArgumentException
- if exceptions
is empty
Copyright © 2011 Google. All Rights Reserved.