On Thu, 11 Jun 2026 18:45:06 GMT, Ashay Rane <[email protected]> wrote:

> Every time an exception object is created, the constructor calls
> `fillInStackTrace()`, which has to walk the call stack to record every
> frame.  This is expensive, and it's also unnecessary if the exception is
> never thrown.  There are a few instances in various parts of the JDK
> libraries where we create an exception in the dominator block but don't
> always throw it.  This patch fixes those cases so that if the exception
> is not going to be thrown, it is never created in the first place.
> 
> ---------
> - [x] I confirm that I make this contribution in accordance with the [OpenJDK 
> Interim AI Policy](https://openjdk.org/legal/ai).

src/java.base/share/classes/sun/security/tools/keytool/Main.java line 1:

> 1: /*

FWIW, the intent behind having a single site where an exception is created is 
to avoid repetitive `throw new 
IOException(rb.getString("Illegal.startdate.value"))` calls. The implementation 
of the intent came with the cost, which this PR mitigates, but defeats the 
intent :)

I'd create a helper

private static IOException createIllegalStartDateException() {
    return new IOException(rb.getString("Illegal.startdate.value"));
}


And replace every

throw ioe;


with

throw createIllegalStartDateException();


This way, the issue of creating exceptions that won't be thrown is addressed 
without code duplication.

Alternatively, to keep the fix local, you can use a lambda instead of the 
helper method:

Supplier<IOException> ioe = () -> {
    return new IOException(rb.getString("Illegal.startdate.value"));
};


Usage:

throw ioe.get();

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/31487#discussion_r3403216139

Reply via email to