Sneaky Throws question
Closed this issue · 2 comments
Hello,
This is not an issue per-se, just wondering why the implementation of ExceptionSmuggler
is not actually more like this for example:
final class ExceptionSmuggler extends CheckedExceptionHandler {
@Override
public RuntimeException handle(Exception exception) {
return sneakyThrow(exception);
}
static <T extends Throwable> RuntimeException sneakyThrow(Throwable t) throws T {
throw (T) t;
}
}
you do want a "sneaky" throw after all here...
also I've actually wondered why CheckedExceptionHandler::function
for example, is not a lambda too, in the form:
public final <T, R> Function<T, R> function(ThrowingFunction<T, R> function) {
return t -> {
try {
return function.apply(t);
} catch (RuntimeException exception) {
throw exception;
} catch (Exception exception) {
throw handle(exception);
}
};
}
And the last nitpick is that those constants in Exceptions
should really be upper-case (it's just easier to read since it's a common convention)... :
private static final Logger LOGGER = LoggerFactory.getLogger(Exceptions.class);
The thing is, we started looking for something like this for our code and while evaluating some other projects on github, your code seems the closest I would accept. I could submit a PR with these changes if you agree with them.
Eugene.
-
Simplified
ExceptionSmuggler
: Thanks, I have tested this locally. It worked and I committed and pushed something similar to your code. It's a bit surprising it works without the explicit type. I hope this is not just Eclipse oddity and it will compile smoothly during next release. -
@SuppressWarnings("unchecked")
: This has to stay as otherwise there's a warning about the unchecked exception type cast. -
Classses like
CheckedFunction
are used instead of lambdas in order to have something nicer in stack traces and in debugger. It's better the way it is. -
Naming conventions are mine. I don't uppercase non-integer constants.
I like your reasonings, thx for accepting (1). (2) is something that my IDE got rid of while copy pasting. (3) is something I did not think about, I am not sure I care about this, but it's your project after all... It does not matter anyway, because this is a capturing lambda, an instance would be created on every call anyway - so your code vs lambda does not bring any benefit. (4) - weird :) but again, your project. thx for the time.