java 从 String.getBytes("UTF-8") 处理 UnsupportedEncodingException 的推荐方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10748440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recommended method for handling UnsupportedEncodingException from String.getBytes("UTF-8")
提问by cqcallaw
What is the recommended way to handle an UnsupportedEncodingException when calling String.getBytes("UTF-8") inside a library method?
在库方法中调用 String.getBytes("UTF-8") 时,处理UnsupportedEncodingException的推荐方法是什么?
If I'm reading http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.htmlcorrectly, UTF-8 encoding should alwaysbe available, which leads me to believe there's no reason to pass this exception on to the library's consumer (that is, add a throws
clause to the method signature). It seems any failure mode that made UTF-8 encoding facilities unavailable would be catastrophic, leading me to write this handler:
如果我正在正确阅读http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html,则 UTF-8 编码应该始终可用,这让我相信没有将此异常传递给库的使用者的原因(即,throws
向方法签名添加子句)。任何使 UTF-8 编码工具不可用的故障模式似乎都是灾难性的,导致我编写了这个处理程序:
try
{
....
return "blah".getBytes("UTF-8");
}
catch (UnsupportedEncodingException e)
{
// we're assuming UTF-8 encoding is always available.
// see
// http://docs.oracle.com/javase/6/docs/technotes/guides/intl/encoding.doc.html
e.printStackTrace();
return null; //prevent compile-time "method must return a result" errors
}
Is there a failure mode that wouldn't be addressed by this snippet?
是否存在此代码段无法解决的故障模式?
回答by Judge Mental
You know what I do?
你知道我在做什么吗?
return "blah".getBytes( Charset.forName( "UTF-8" ) );
This one doesn't throw a checked exception.
这个不会抛出已检查的异常。
Update: Since Java 1.7, we have StandardCharsets.
更新:从 Java 1.7 开始,我们有了StandardCharsets。
return "blah".getBytes( StandardCharsets.UTF_8 );
回答by spaaarky21
I ran across this question while trying to figure out if UTF-8 is always available. So thanks for the link.
我在试图弄清楚 UTF-8 是否始终可用时遇到了这个问题。所以谢谢你的链接。
I agree that there is no need to throw a checkedexception when it comes to encoding and decoding using a specific character set that is guaranteed to be available. If the character set was a variable that was passed in, I would probably throw UnsupportedEncodingException.
我同意在使用保证可用的特定字符集进行编码和解码时,没有必要抛出已检查的异常。如果字符集是传入的变量,我可能会抛出 UnsupportedEncodingException。
This is what I am doing in a similar piece of Android code:
这就是我在一段类似的 Android 代码中所做的:
public static String encode(String input) {
try {
return URLEncoder.encode(input, CharEncoding.UTF_8);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
CharEncoding.UTF_8
is just Apache Commons' String constant for "UTF-8".
CharEncoding.UTF_8
只是 Apache Commons 的“UTF-8”字符串常量。
Judge Mental's suggestion to use StandardCharsets.UTF_8
is great but for those of us doing Android development, it's only available on SDK 19 (KitKat) and above.
Judge Mental 的使用建议StandardCharsets.UTF_8
很好,但对于我们这些从事 Android 开发的人来说,它仅适用于 SDK 19 (KitKat) 及更高版本。