Class Throwables

java.lang.Object
be.imgn.gaia.core.base.Throwables

public final class Throwables extends Object
Creates exceptions from the exception that caused them.

The methods return the exception rather than throw it, so that the caller throws it: the compiler then knows that the code after it is not reached.

int port;
try {
    port = Integer.parseInt(text);
} catch (NumberFormatException e) {
    throw Throwables.illegalArgument(e, "Not a port: %s", text);
}

A message format follows the syntax of String.format(Locale, String, Object...) and is formatted in Locale.ROOT, so that messages do not depend on the default locale.

Since:
0.1.1
  • Method Details

    • illegalArgument

      public static IllegalArgumentException illegalArgument(Throwable cause, @Nullable Object message)
      Creates an exception that reports an illegal argument, caused by another exception.
      Parameters:
      cause - the exception that caused it
      message - the message of the exception, converted with String.valueOf(Object)
      Returns:
      a new exception, for the caller to throw
    • illegalArgument

      @FormatMethod public static IllegalArgumentException illegalArgument(Throwable cause, @FormatString String messageFormat, @Nullable Object... params)
      Creates an exception that reports an illegal argument, caused by another exception.
      Parameters:
      cause - the exception that caused it
      messageFormat - the format of the message of the exception
      params - the values the format refers to
      Returns:
      a new exception, for the caller to throw
      Throws:
      IllegalFormatException - if messageFormat is not a valid format for params
    • illegalState

      public static IllegalStateException illegalState(Throwable cause, @Nullable Object message)
      Creates an exception that reports an illegal state, caused by another exception.
      Parameters:
      cause - the exception that caused it
      message - the message of the exception, converted with String.valueOf(Object)
      Returns:
      a new exception, for the caller to throw
    • illegalState

      @FormatMethod public static IllegalStateException illegalState(Throwable cause, @FormatString String messageFormat, @Nullable Object... params)
      Creates an exception that reports an illegal state, caused by another exception.
      Parameters:
      cause - the exception that caused it
      messageFormat - the format of the message of the exception
      params - the values the format refers to
      Returns:
      a new exception, for the caller to throw
      Throws:
      IllegalFormatException - if messageFormat is not a valid format for params