Exceptions should not be created without being thrown

Java pattern

Creating a new Throwable without actually throwing or binding it is useless and is probably due to a mistake.


Apply with the Grit CLI
grit apply no_unthrown_exceptions

Throws unthrown exception

BEFORE
class BadClass {
    public String doSomething(int x) {
        if (x < 0) {
            new IllegalArgumentException("x must be nonnegative");
        }
        if (x > 100) {
            throw new IllegalArgumentException("x must be less than 100");
        }
        IllegalArgumentException saveIt = new IllegalArgumentException("Don't correct this");
        new NotAThrowable();
        return "some-string";
    }
}
AFTER
class BadClass {
    public String doSomething(int x) {
        if (x < 0) {
            throw new IllegalArgumentException("x must be nonnegative");
        }
        if (x > 100) {
            throw new IllegalArgumentException("x must be less than 100");
        }
        IllegalArgumentException saveIt = new IllegalArgumentException("Don't correct this");
        new NotAThrowable();
        return "some-string";
    }
}