class Example {
void method() {
if (doSomething())
throw ExceptionThrower.exception();
}
}
Interface ExceptionThrower<K extends ExceptionContext.Key>
- Type Parameters:
K- Type of key to accessExceptionContext.
- All Known Implementing Classes:
SymfonionExceptionThrower
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.
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 Summary
Modifier and TypeMethodDescriptionReturns a context manager of this object.default RuntimeExceptionThrows aRuntimeExceptionwith information from the current context.default RuntimeExceptionThrows aRuntimeExceptionwith information from the current context following a givenmessage.default RuntimeExceptionThrows aRuntimeExceptionwith information from the current context following a givenmessage.default RuntimeExceptionThrows aRuntimeExceptionwith information from the current context.message(ExceptionContext<K> context) Composes a message string using contextual information stored in a givencontext.default RuntimeExceptionnewException(String message) Creates an exception with a givenmessage.newException(String message, Throwable cause) Creates an exception with a givenmessageand acause.
-
Method Details
-
exception
Throws a
RuntimeExceptionwith information from the current context.- Returns:
- This method never returns.
-
exception
Throws a
RuntimeExceptionwith information from the current context following a givenmessage.- Parameters:
message- A message in the exception- Returns:
- This method never returns.
-
exception
Throws a
RuntimeExceptionwith information from the current context.causewill be nested in the exception.- Parameters:
cause- A nested exception.- Returns:
- This method never returns.
-
exception
Throws a
RuntimeExceptionwith information from the current context following a givenmessage.causewill be nested in the exception.- Parameters:
message- A message in the exceptioncause- A nested exception.- Returns:
- This method never returns.
-
newException
Creates an exception with a given
message. This method internally callsnewException(message, null).- Parameters:
message- A message set to the exception- Returns:
- This method never returns.
-
newException
Creates an exception with a given
messageand acause.causeis 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
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:
-