Package 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:
Serializable
An
ClusterException
is a 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 ClusterException.create(exceptions); } }
See semantic details at create(Collection)
.
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
ClusterException
(Collection<? extends Throwable> exceptions) -
Method Summary
Modifier and TypeMethodDescription(package private) static RuntimeException
Seecreate(Collection)
.(package private) static RuntimeException
create
(Collection<? extends Throwable> exceptions) Given a collection of exceptions, returns aRuntimeException
, with the following rules: Ifexceptions
has a single exception and that exception is aRuntimeException
, return it Ifexceptions
has a single exceptions and that exceptions is not aRuntimeException
, return a simpleRuntimeException
that wraps it Otherwise, return an instance ofClusterException
that wraps the first exception in theexceptions
collection.Methods inherited from class java.lang.Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
-
Field Details
-
exceptions
-
-
Constructor Details
-
ClusterException
-
-
Method Details
-
create
Seecreate(Collection)
. -
create
Given a collection of exceptions, returns aRuntimeException
, with the following rules:- If
exceptions
has a single exception and that exception is aRuntimeException
, return it - If
exceptions
has a single exceptions and that exceptions is not aRuntimeException
, return a simpleRuntimeException
that wraps it - Otherwise, return an instance of
ClusterException
that wraps the first exception in theexceptions
collection.
Though this method takes any
Collection
, it often makes most sense to pass aList
or some other collection that preserves the order in which the exceptions got added.- Throws:
NullPointerException
- ifexceptions
is nullIllegalArgumentException
- ifexceptions
is empty
- If
-