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

Ichiroh Takiguchi itakiguchi at openjdk.java.net
Fri Oct 22 00:29:03 UTC 2021


On Thu, 21 Oct 2021 18:00:46 GMT, Naoto Sato <naoto at openjdk.org> wrote:

>> I'm not reviewer.
>> 
>> I'd like to write down following code without `try-catch`.
>> 
>> var cs = Charset.forName(charsetName, null);
>> if (cs == null) cs = StandardCharsets.UTF_8;
>> 
>> or please find out more easy way.
>
>> I'd like to write down following code without `try-catch`.
> 
> You don't *have to* try-catch those exceptions if you are not interested, as they are subclasses of `RuntimeException`.
> 
>> ```
>> var cs = Charset.forName(charsetName, null);
>> if (cs == null) cs = StandardCharsets.UTF_8;
>> ```
> 
> This could be simplified to
> 
> 
> var cs = Charset.forName(charsetName, StandardCharsets.UTF_8);

@naotoj 
Oh sorry, I'd like to detect fallback charset is used, like: 

var cs = Charset.forName(charsetName, null);
if (cs == null) {
   System.err.println("Used UTF-8 encoding instead of "+charsetName+");
   cs = StandardCharsets.UTF_8;
}

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

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


More information about the core-libs-dev mailing list