Class Throwables

java.lang.Object
com.atlassian.bamboo.utils.Throwables

public class Throwables extends Object
Convenience methods for working with Throwables.
Since:
10.0
  • Constructor Details

    • Throwables

      public Throwables()
  • Method Details

    • throwIfUnchecked

      public static void throwIfUnchecked(Throwable throwable)
      Rethrow the given throwable if it is an instance of RuntimeException or Error, i.e., if it is unchecked.
      Parameters:
      throwable -
    • getCausalChain

      public static List<Throwable> getCausalChain(Throwable throwable)
      Copy from com.google.common.base.Throwables.propagateIfInstanceOf.

      Gets a Throwable cause chain as a list. The first entry in the list will be throwable followed by its cause hierarchy. Note that this is a snapshot of the cause chain and will not reflect any subsequent changes to the cause chain.

      Here's an example of how it can be used to find specific types of exceptions in the cause chain:

       Iterables.filter(Throwables.getCausalChain(e), IOException.class));
       
      Parameters:
      throwable - the non-null Throwable to extract causes from
      Returns:
      an unmodifiable list containing the cause chain starting with throwable
      Throws:
      IllegalArgumentException - if there is a loop in the causal chain
    • throwIfInstanceOf

      public static <X extends Throwable> void throwIfInstanceOf(Throwable throwable, Class<X> declaredType) throws X
      Copy from com.google.common.base.Throwables.propagateIfInstanceOf.

      Throws throwable if it is an instance of declaredType. Example usage:

       for (Foo foo : foos) {
         try {
           foo.bar();
         } catch (BarException | RuntimeException | Error t) {
           failure = t;
         }
       }
       if (failure != null) {
         throwIfInstanceOf(failure, BarException.class);
         throwIfUnchecked(failure);
         throw new AssertionError(failure);
       }
       
      Throws:
      X extends Throwable