de.larssh.utils.test.SneakyException Maven / Gradle / Ivy
Show all versions of utils-test Show documentation
package de.larssh.utils.test;
import de.larssh.utils.annotations.PackagePrivate;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* SneakyException allow rethrowing any kind of {@link Exception}, hiding them
* from compiler checks.
*
*
* The calling method should either:
*
* - have a {@code throws} statement for the hidden exceptions
*
- handle hidden exceptions using {@code catch}
*
- or at least document the hidden exceptions in JavaDoc using
* {@code @throws}.
*
*
*
* This class has been copied from "utils" module and must not be used
* publicly!
*/
@PackagePrivate
final class SneakyException extends RuntimeException {
/**
* Rethrows {@code throwable}, hiding it from compiler checks.
*
*
* The calling method should either:
*
* - have a {@code throws} statement for the hidden exceptions
*
- handle hidden exceptions using {@code catch}
*
- or at least document the hidden exceptions in JavaDoc using
* {@code @throws}.
*
*
* @param throwable throwable to be hidden
*/
@PackagePrivate
SneakyException(final Throwable throwable) {
super(throwable);
rethrow(throwable);
}
/**
* Rethrows {@code throwable}, hiding it from compiler checks.
*
*
* This is not part of the constructor to hide the public API from generic type
* {@code T}.
*
* @param generic throwable
* @param throwable throwable to be hidden
* @throws T hidden throwable
*/
@SuppressWarnings("unchecked")
@SuppressFBWarnings(value = "THROWS_METHOD_THROWS_CLAUSE_THROWABLE",
justification = "this is the way sneaky exceptions work")
private void rethrow(final Throwable throwable) throws T {
throw (T) throwable;
}
}