Is Java UTF-8 Charset exception possible? -
can java possibly throw unsupportedencodingexception
when using "utf-8"
encoding, or can safely suppress it's throwing?
as mcdowell noted in comment templatetypdef's answer: if use charset
object when instantiate new string
instead of passing name of charset, don't have deal unsupportedencodingexception
or other checked exception:
byte[] bytes = ...; // requires handle unsupportedencodingexception string s1 = new string(bytes, "utf-8"); // doesn't require handle checked exceptions string s2 = new string(bytes, charset.forname("utf-8"));
it's inconsistency in java's standard library have live with...
note charset.forname(...)
can throw exceptions (illegalcharsetnameexception
, illegalargumentexception
, unsupportedcharsetexception
), these unchecked exceptions, don't have catch or re-throw them yourself.
edit - since java 7 there's class java.nio.charset.standardcharsets
has constants used character encodings. example:
string s3 = new string(bytes, standardcharsets.utf_8);
Comments
Post a Comment