Interface ExceptionThrower<K extends ExceptionContext.Key>

Type Parameters:
K - Type of key to access ExceptionContext.
All Known Implementing Classes:
SymfonionExceptionThrower

public interface ExceptionThrower<K extends ExceptionContext.Key>

A class to throw exceptions with contextual and informative messages.

Methods in this class never return and throw exceptions instead. The return type of theirs is a "placeholder" and to enable programmers to let the compiler (linter) know a method stops there by putting a throw statement.

That is, you can do the following.

class Example {
void method() {
if (doSomething())
throw ExceptionThrower.exception();
}
}

Suppose if we have a method void throwException(),

if (somethingFails)
throwException();
nextStatement();

This confuses a compiler and static code analyzer so that it assumes nextStatement() may be executed.

See Also:
  • Method Details

    • exception

      default RuntimeException exception()

      Throws a RuntimeException with information from the current context.

      Returns:
      This method never returns.
    • exception

      default RuntimeException exception(String message)

      Throws a RuntimeException with information from the current context following a given message.

      Parameters:
      message - A message in the exception
      Returns:
      This method never returns.
    • exception

      default RuntimeException exception(Throwable cause)

      Throws a RuntimeException with information from the current context. cause will be nested in the exception.

      Parameters:
      cause - A nested exception.
      Returns:
      This method never returns.
    • exception

      default RuntimeException exception(String message, Throwable cause)

      Throws a RuntimeException with information from the current context following a given message. cause will be nested in the exception.

      Parameters:
      message - A message in the exception
      cause - A nested exception.
      Returns:
      This method never returns.
    • newException

      default RuntimeException newException(String message)

      Creates an exception with a given message. This method internally calls newException(message, null).

      Parameters:
      message - A message set to the exception
      Returns:
      This method never returns.
    • newException

      RuntimeException newException(String message, Throwable cause)

      Creates an exception with a given message and a cause. cause is supposed to be nested in the created exception. By overriding this method, you can make this object throw your custom exceptions.

      Parameters:
      message - A message string of the exception.
      cause - A nested exception.
      Returns:
      You can return the created exception but not obliged to
    • message

      String message(ExceptionContext<K> context)

      Composes a message string using contextual information stored in a given context.

      Parameters:
      context - A context that stores contextual information.
      Returns:
      A composed message.
    • contextManager

      ExceptionContext.Manager<K> contextManager()

      Returns a context manager of this object.

      Returns:
      A context manager of this object.
      See Also: