All Downloads are FREE. Search and download functionalities are using the official Maven repository.

brooklyn.util.exceptions.Exceptions Maven / Gradle / Ivy

Go to download

Utility classes and methods developed for Brooklyn but not dependendent on Brooklyn or much else

There is a newer version: 0.7.0-M1
Show newest version
package brooklyn.util.exceptions;

import static com.google.common.base.Predicates.instanceOf;
import static com.google.common.base.Throwables.getCausalChain;
import static com.google.common.collect.Iterables.find;

import java.util.NoSuchElementException;

import com.google.common.base.Throwables;

public class Exceptions {

    /**
     * Propagate a {@link Throwable} as a {@link RuntimeException}.
     * 

* Like Guava {@link Throwables#propagate(Throwable)} but throws {@link RuntimeInterruptedException} * to handle {@link InterruptedException}s. */ public static RuntimeException propagate(Throwable throwable) { if (throwable instanceof InterruptedException) throw new RuntimeInterruptedException((InterruptedException) throwable); return Throwables.propagate(throwable); } /** * Propagate exceptions which are fatal. *

* Propagates only those exceptions which one rarely (if ever) wants to capture, * such as {@link InterruptedException} and {@link Error}s. */ public static void propagateIfFatal(Throwable throwable) { if (throwable instanceof InterruptedException) throw new RuntimeInterruptedException((InterruptedException) throwable); if (throwable instanceof Error) throw (Error) throwable; } // based on jclouds Throwables2 (with guice removed) @SuppressWarnings("unchecked") public static T getFirstThrowableOfType(Throwable from, Class clazz) { try { return (T) find(getCausalChain(from), instanceOf(clazz)); } catch (NoSuchElementException e) { return null; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy