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:
java.io.Serializable
@GwtCompatible final class ClusterException extends java.lang.RuntimeException
AnClusterExceptionis 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 Modifier and Type Field Description java.util.Collection<? extends java.lang.Throwable>exceptions
-
Constructor Summary
Constructors Modifier Constructor Description privateClusterException(java.util.Collection<? extends java.lang.Throwable> exceptions)
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.RuntimeExceptioncreate(java.lang.Throwable... exceptions)static java.lang.RuntimeExceptioncreate(java.util.Collection<? extends java.lang.Throwable> exceptions)Given a collection of exceptions, returns aRuntimeException, with the following rules: Ifexceptionshas a single exception and that exception is aRuntimeException, return it Ifexceptionshas a single exceptions and that exceptions is not aRuntimeException, return a simpleRuntimeExceptionthat wraps it Otherwise, return an instance ofClusterExceptionthat wraps the first exception in theexceptionscollection.
-
-
-
Method Detail
-
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 aRuntimeException, with the following rules:- If
exceptionshas a single exception and that exception is aRuntimeException, return it - If
exceptionshas a single exceptions and that exceptions is not aRuntimeException, return a simpleRuntimeExceptionthat wraps it - Otherwise, return an instance of
ClusterExceptionthat wraps the first exception in theexceptionscollection.
Though this method takes any
Collection, it often makes most sense to pass aListor some other collection that preserves the order in which the exceptions got added.- Throws:
java.lang.NullPointerException- ifexceptionsis nulljava.lang.IllegalArgumentException- ifexceptionsis empty
- If
-
-