Class Exceptions

java.lang.Object
com.github.tomakehurst.wiremock.common.Exceptions

public class Exceptions extends Object
  • Constructor Details

    • Exceptions

      public Exceptions()
  • Method Details

    • throwUnchecked

      public static <T> T throwUnchecked(Throwable ex, 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(Throwable ex)
      Parameters:
      ex - The exception that will be thrown, unwrapped and unchecked
    • uncheck

      public static <T> T uncheck(Callable<T> work, Class<T> returnType)
    • uncheck

      public static void uncheck(Exceptions.RunnableWithException work)
    • renderStackTrace

      public static String renderStackTrace(Throwable t)