-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Refactoring request (Kernel32.java
)
#1542
Comments
My personal feeling: Don't fix it if it is not broken. I'm inclined not to waste time on beautification of code, that noone will touch in the future again. |
But this feels wrong: Currently, when the So, unless I'm missing something here, the current "re-throw to invoke finally block" effectively does nothing, because in all possible cases the thrown exception is going to be discarded shortly thereafter by the Note:
Also, I would like to refactor the repeating clean-up code into a separate method, because otherwise this pull request would require to add even more "1:1" copies of the very same clean-up code in order to implement proper error handling 🤔 |
If you find someone to review and merge this, fine with me, I will not be that someone. |
I might consider reviewing this, but I need to be absolutely sure nothing is going to be broken. As @matthiasblaesing said, "Don't fix it if it is not broken." Translated to US Southern Slang, "If it ain't broke, don't fix it.". Before even starting, I'd like you to explain what happens in JDK 1.6 and what happens later JDKs with this odd situation of both throwing an exception in the catch block and throwing that exact same object in the finally block. Which one actually reaches the user? Did it change with JDK 1.7's exception handling changes? If you do refactor, ease my review:
|
I don't think it's an implementation choice. It's clear from the language spec that:
See here: (Note that in this text "completes abruptly" actually means "throws an exception", as opposed to "completes normally") |
Like this? |
Wasn't easy to find a working download of JDK 1.6 (Java 6) in 2023, but here we go! Consider this example: public class ExceptionTest {
public static class MyException extends Exception {
private static final long serialVersionUID = 1L;
public MyException(final String message) {
super(message);
}
}
public static void main(String[] args) {
System.err.println("Running on Java v" + System.getProperty("java.version"));
try {
test();
} catch (final Throwable e ) {
System.err.println("EXCEPTION:");
e.printStackTrace();
}
System.err.println("goodbye!");
}
private static void test() throws MyException {
MyException err = null;
try {
System.err.println("now executing \"try\"");
throw new MyException("A");
} catch(MyException e) {
System.err.println("now executing \"catch\"");
err = e;
} finally {
System.err.println("now executing \"finally\"");
try {
throw new MyException("B");
} catch (MyException e) {
if (err != null) {
//err.addSuppressed(e); <-- not supported by JDK 1.6
} else {
err = e;
}
}
if (err != null) {
throw err;
}
}
}
} JDK 1.6 output is exactly as expected, according to the language spec:
Note: No Output from up-to-date JDK 17 is the same: (except that, with an up-to-date JDK, we can add the "suppressed" exception to the actual exception)
|
Thanks for digging that up. That's what I needed.
Precisely. Looking forward to your PR. |
Closed via #1544 |
In
Kernel32Util.java
there are many functions that have the following form:First of all, I think this line in the
catch
block is superfluous/redundant:That is because the
finally
block will be executed, regardless of whether there was an exception or not. Even if there was an exception and if that exception was caught by acatch
block, thefinally
block still is always going to be executed. Also, because of theif (we != null) {throw we}
inside thefinally
block, the exception is guaranteed to be thrown eventually...Furthermore, because the above pattern occurs many times, I'd suggest refactoring it to:
This should eliminate a whole lot of "copy & paste" code 😏
The text was updated successfully, but these errors were encountered: