-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ExternalResource: declare after() to throw Exception #1421
ExternalResource: declare after() to throw Exception #1421
Conversation
So that clients don't have to try-catch-and-rethrow explicitly checked exceptions.
*/ | ||
protected void after() { | ||
protected void after() throws Exception { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why Exception
instead of Throwable
like we did for before()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just because AFAIK rarely methods throw Throwable, it's more likely they throw Exception, e.g. AutoCloseable#close
.
But ya, we should probably make it more generic and throw Throwable.
Do you want me to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it's rare for methods to throw Throwable
, when you call a method using reflection you often handle the InvocationTargetException
by throwing the cause (a Throwable
). That's why Statement.evaluate()
is declared to throw Throwable
.
I don't think it harms anything to have the method declared to throw Throwable
. The calling code catches Throwable
and subclasses of ExternalResource
can override the method to throw a different exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that closing a resource usually involves doing reflection or anything that throws Throwable, but anyway yeah, it's harmless, so I'll do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I will wait for another maintainer to review this before we squash and merge.
Merged. @alb-i986 could you please update the release notes? |
Release notes updated. Cheers |
This allows clients to call methods that throw checked exceptions without having to catch and wrap checked exceptions.
@marcphilipp This breaks code that extends ExternalResource, overrides |
Agreed! @kcooney Do you have time to do so? |
@marcphilipp I thought I might have time but apparently not 🙁 |
Revert commit cebbf5e. It breaks code that extends ExternalResource, overrides after() and calls super.after() in after().
This allows clients to call methods that throw checked exceptions without having to catch and wrap checked exceptions.
So that clients don't have to try-catch-and-rethrow explicitly checked exceptions.
You may not be happy with changing the signature, though :(