h*****0 发帖数: 4889 | |
g*****g 发帖数: 34805 | 2 b will give you a compilation error.
【在 h*****0 的大作中提到】 : 下面例子中,b处加还是不加?
|
h*****0 发帖数: 4889 | 3 what if I deleted b?
【在 g*****g 的大作中提到】 : b will give you a compilation error.
|
m******t 发帖数: 2416 | 4
I don't think you need b. UTF would
remain null if an exception is thrown.
The bigger question, though, is whether
you really want to skip the error if UTF
fails to load.
【在 h*****0 的大作中提到】 : what if I deleted b?
|
h*****0 发帖数: 4889 | 5 if some jvm platform doesn't support utf-8, i would let the program die
without hesitation.
【在 m******t 的大作中提到】 : : I don't think you need b. UTF would : remain null if an exception is thrown. : The bigger question, though, is whether : you really want to skip the error if UTF : fails to load.
|
h*****0 发帖数: 4889 | 6 and if I simply delete b, the variable UTF may not be initiated, compile
error.
【在 m******t 的大作中提到】 : : I don't think you need b. UTF would : remain null if an exception is thrown. : The bigger question, though, is whether : you really want to skip the error if UTF : fails to load.
|
m******t 发帖数: 2416 | 7
That's the opposite of what you are doing at c though, isn't it?
【在 h*****0 的大作中提到】 : if some jvm platform doesn't support utf-8, i would let the program die : without hesitation.
|
h*****0 发帖数: 4889 | 8 what do you mean? c is the position why I use UTF, and if the platform doesn
't support it, the program will die at c.
【在 m******t 的大作中提到】 : : That's the opposite of what you are doing at c though, isn't it?
|
m******t 发帖数: 2416 | 9
doesn
Unless I'm missing something, you are relying on String.getBytes
to blow up upon a null charset name. That might work, but it's not
explicitly defined in the API, so I wouldn't trust it.
If you _want_ it to blow up, why not just let it blow up at
b then, instead of swallowing the exception there?
【在 h*****0 的大作中提到】 : what do you mean? c is the position why I use UTF, and if the platform doesn : 't support it, the program will die at c.
|
Z****e 发帖数: 2999 | 10 agree, in general, just avoid catching RuntimeExceptions
【在 m******t 的大作中提到】 : : doesn : Unless I'm missing something, you are relying on String.getBytes : to blow up upon a null charset name. That might work, but it's not : explicitly defined in the API, so I wouldn't trust it. : If you _want_ it to blow up, why not just let it blow up at : b then, instead of swallowing the exception there?
|
|
|
h*****0 发帖数: 4889 | 11 you make a good point. I can(maybe I should) add code at b to blow it up,
which would indicate that "UTF not supported, my program don't want to run
anymore". However, this doesn't solve the "variable not initiated" problem.
【在 m******t 的大作中提到】 : : doesn : Unless I'm missing something, you are relying on String.getBytes : to blow up upon a null charset name. That might work, but it's not : explicitly defined in the API, so I wouldn't trust it. : If you _want_ it to blow up, why not just let it blow up at : b then, instead of swallowing the exception there?
|
h*****0 发帖数: 4889 | 12 now I know what you mean. But my problem is actually about how to satisfy
the compiler.
【在 m******t 的大作中提到】 : : doesn : Unless I'm missing something, you are relying on String.getBytes : to blow up upon a null charset name. That might work, but it's not : explicitly defined in the API, so I wouldn't trust it. : If you _want_ it to blow up, why not just let it blow up at : b then, instead of swallowing the exception there?
|
m******t 发帖数: 2416 | 13
Well you could solve that problem easily by not making
it final.
【在 h*****0 的大作中提到】 : you make a good point. I can(maybe I should) add code at b to blow it up, : which would indicate that "UTF not supported, my program don't want to run : anymore". However, this doesn't solve the "variable not initiated" problem.
|
h*****0 发帖数: 4889 | 14 by logic, I should make it final "in case it will be changed accidentally
somewhere else".:)
【在 m******t 的大作中提到】 : : Well you could solve that problem easily by not making : it final.
|
r*****l 发帖数: 2859 | 15 define a class, something like this:
public class FinalUTF8CharSet {
private CharSet charSet = null;
private FinalUTF8CharSet _instance = null;
private FinalUTF8CharSet() {
try {
charSet = CharSet.forName("UTF-8");
} catch (Exception e) {
// leave charSet as null
}
}
public static FinalUTF8CharSet getInstance() {
if (_instance == null) {
_instance = new FinalUTF8CharSet();
}
return _instance;
【在 h*****0 的大作中提到】 : by logic, I should make it final "in case it will be changed accidentally : somewhere else".:)
|
h*****0 发帖数: 4889 | 16 多谢多谢。
我自己的办法是把原来的代码稍改一下
static {
Charset utf = null;
try {
utf = Charset.forName("UTF-8");
} catch (Exception e) {
//maybe add throw a runtime exception to let program die now
}
UTF = utf;
}
这样不管是编译器还是我都高兴了。
【在 r*****l 的大作中提到】 : define a class, something like this: : public class FinalUTF8CharSet { : private CharSet charSet = null; : private FinalUTF8CharSet _instance = null; : private FinalUTF8CharSet() { : try { : charSet = CharSet.forName("UTF-8"); : } catch (Exception e) { : // leave charSet as null : }
|
m******t 发帖数: 2416 | 17
It's _private_ static, man. Nobody can accidentally change
it unless you let them. 8-)
But I'm glad both you and the compiler are happy now.
【在 h*****0 的大作中提到】 : by logic, I should make it final "in case it will be changed accidentally : somewhere else".:)
|