Class Exceptions


  • public class Exceptions
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      Exceptions()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.String renderStackTrace​(java.lang.Throwable t)  
      static void throwUnchecked​(java.lang.Throwable ex)  
      static <T> T throwUnchecked​(java.lang.Throwable ex, java.lang.Class<T> returnType)
      Because this method throws an unchecked exception, when it is called in a method with a return type the compiler does not know the method is exiting, requiring a further line to return null or throw an unchecked exception directly.
      static void uncheck​(Exceptions.RunnableWithException work)  
      static <T> T uncheck​(java.util.concurrent.Callable<T> work, java.lang.Class<T> returnType)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Exceptions

        public Exceptions()
    • Method Detail

      • throwUnchecked

        public static <T> T throwUnchecked​(java.lang.Throwable ex,
                                           java.lang.Class<T> returnType)
        Because this method throws an unchecked exception, when it is called in a method with a return type the compiler does not know the method is exiting, requiring a further line to return null or throw an unchecked exception directly. This generified method allows this to be avoided by tricking the compiler by adding a return statement as so:
             String someMethod() {
                 try {
                     somethingThatThrowsException();
                 } catch (Exception e) {
                     return throwUnchecked(e, String.class); // does not actually return, throws the exception
                 }
             }
         
        Parameters:
        ex - The exception that will be thrown, unwrapped and unchecked
        returnType - trick to persuade the compiler that a method returns appropriately
        Returns:
        Never returns, always throws the passed in exception
      • throwUnchecked

        public static void throwUnchecked​(java.lang.Throwable ex)
        Parameters:
        ex - The exception that will be thrown, unwrapped and unchecked
      • uncheck

        public static <T> T uncheck​(java.util.concurrent.Callable<T> work,
                                    java.lang.Class<T> returnType)
      • renderStackTrace

        public static java.lang.String renderStackTrace​(java.lang.Throwable t)