Is there a way to annotate a method so all exceptions thrown are converted to runtime exception automagically?
@MagicAnnotation
// no throws clause!
void foo()
{
throw new Exception("bar")'
}
Wrapping exception simply involves setting the InnerException property of the new exception to propagate up the call stack, to the caught exception. Doing this maintains the exception call stack information. You can see that the caught exception was the ArithmeticException and it had the defined error message.
You can write a helper function to convert thrown exceptions to unchecked ones. Essentially you would call something like callUnchecked(() -> getConfigFactory()) in your code and define callUnchecked similar to try { return supplier. get(); } catch (Throwable ex) { throw new RuntimeException(ex); } .
2. Re: try catch around annotated method. Right approach, but Seam doesn't support method level interceptors, only class level interceptors. So you can attach the interceptor at class level, and then mark the method with the same annotation, and only intercept those.
Project Lombok's @SneakyThrows
is probably what you are looking for. Is not really wrapping your exception (because it can be a problem in a lot of cases), it just doesn't throw an error during compilation.
@SneakyThrows
void foo() {
throw new Exception("bar")'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With