Class Throwables

    • Method Detail

      • chain

        public static <T extends Throwable> T chain​(T throwable,
                                                    Throwable cause)
        Simply call throwable.initCause(cause) and return throwable. This makes it easy to chain for old exception types with no chained constructor.

        throw Throwables.chain(new MyExceptionTypeWithNoChainedConstructor(cause.getMessage()), cause)

        instead of

        MyExceptionTypeWithNoChainedConstructor throwThis = new MyExceptionTypeWithNoChainedConstructor(cause.getMessage);
        throwThis.initCause(cause);
        throw throwThis;

      • hasCauseInCausalChain

        public static <T extends Throwable> boolean hasCauseInCausalChain​(T throwable,
                                                                          Class<? extends T> causeClass)
        Returns true iff an exception of type causeClass exists somewhere in the causal chain.
      • rewrapAndThrowUncheckedException

        public static RuntimeException rewrapAndThrowUncheckedException​(Throwable ex)
        If Throwable is a RuntimeException or Error, rewrap and throw it. If not, throw a PalantirRuntimeException.
      • rewrapAndThrowUncheckedException

        public static RuntimeException rewrapAndThrowUncheckedException​(String newMessage,
                                                                        Throwable ex)
        If Throwable is a RuntimeException or Error, rewrap and throw it. If not, throw a PalantirRuntimeException.
      • unwrapAndThrowAtlasDbDependencyException

        public static AtlasDbDependencyException unwrapAndThrowAtlasDbDependencyException​(Throwable ex)
        If Throwable is a RuntimeException or Error, rethrow it. If its an ExecutionException or InvocationTargetException, extract the cause and process it. Else, throw a PalantirRuntimeException.
      • throwUncheckedException

        public static RuntimeException throwUncheckedException​(Throwable ex)
        Throws the input Throwable if it is a RuntimeException or Error, otherwise wraps it in a PalantirRuntimeException.
      • throwIfUncheckedException

        public static void throwIfUncheckedException​(Throwable ex)
        Will simply rethrow the exception if it is a RuntimeException or an Error
      • rewrapAndThrowIfInstance

        public static <K extends Throwable> void rewrapAndThrowIfInstance​(Throwable t,
                                                                          Class<K> clazz)
                                                                   throws K extends Throwable
        if (t instanceof K) throw Throwables.rewrap((K)t);

        Note: The runtime type of the thrown exception will be the same as t even if clazz is a supertype of t.

        Throws:
        K extends Throwable
      • rewrapAndThrowIfInstance

        public static <K extends Throwable> void rewrapAndThrowIfInstance​(String newMessage,
                                                                          Throwable t,
                                                                          Class<K> clazz)
                                                                   throws K extends Throwable
        if (t instanceof K) throw Throwables.rewrap((K)t);

        Note: The runtime type of the thrown exception will be the same as t even if clazz is a supertype of t.

        Throws:
        K extends Throwable
      • throwIfInstance

        public static <K extends Throwable> void throwIfInstance​(Throwable t,
                                                                 Class<K> clazz)
                                                          throws K extends Throwable
        if (t instanceof K) throw (K)t;

        Note: The runtime type of the thrown exception will be the same as t even if clazz is a supertype of t.

        Throws:
        K extends Throwable
      • rewrap

        public static <T extends Throwable> T rewrap​(T throwable)
        Rewraps the given throwable in a newly created throwable of the same runtime type in order to capture the current thread's stack trace. Use this method when you are about to rethrow a throwable from another thread, for example when throwing Throwable.getCause() after calling Future.get();
      • rewrap

        public static <T extends Throwable> T rewrap​(String newMessage,
                                                     T throwable)
        Rewraps the given throwable in a newly created throwable of the same runtime type in order to capture the current thread's stack trace. Use this method when you are about to rethrow a throwable from another thread, for example when throwing Throwable.getCause() after calling Future.get();
      • getThreadDump

        public static String getThreadDump()
        Returns a dump of all threads.