RFR: 8270490: Charset.forName() taking fallback default value [v2]

Naoto Sato naoto at openjdk.java.net
Fri Oct 22 17:38:08 UTC 2021


On Fri, 22 Oct 2021 02:07:44 GMT, Sergey Bylokhov <serb at openjdk.org> wrote:

>> I first thought of swallowing all exceptions in 2-arg forName(), but decided not to do that. Because `IllegalArgumentException` and `IllegalCharsetNameException` are for the validity of the passed `charsetName`, like detecting `null` or invalid chars like "��". On the other hand, `UnsupportedCharsetException` is for the availability which varies depending on the user's settings and or platform, which can be safely replaced with `fallback` charset. So yes, it is not totally getting rid of `try-catch` but it avoids `UnsupportedCharsetException` which is only detectable at runtime.
>
> Then what is the benefit, if the user will have to write such code anyway?:
> 
>             try {
>                 cs = Charset.forName(StaticProperty.nativeEncoding(), fallback);
>             } catch (Exception ignored) {
>                 cs = fallback;
>             }
> 
> Even in the current code update it can work well w/o the second parameter.

OK, I revised the API to swallow `IllegalCharsetNameException`. This will effectively remove try-catch clauses, as `IllegalArgumentException` is considered an error, and simply avoided by a null check.

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

PR: https://git.openjdk.java.net/jdk/pull/6045


More information about the core-libs-dev mailing list