由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - 我脑袋短路,大家来帮一下:
相关主题
thread safe Singleton 的几种方法?问个exception的问题
一个想不明白的编码问题一个bug竟然是legacy code埋的地雷。
请教个html显示utf8 string的问题 (转载)HashMap一问
请教汉字的utf-8 mapping (转载)NullPointerException 问题
看到一个关于singleton的面试题跪求大牛指点Java,看不懂什么意思。
SpringMVC可否直接处理doGet?question: how to implement this
basic java questiona synchonized problem!
面试的一些题目static getInstance()
相关话题的讨论汇总
话题: utf话题: charset话题: null话题: exception
进入Java版参与讨论
1 (共1页)
h*****0
发帖数: 4889
1
下面例子中,b处加还是不加?
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?

相关主题
SpringMVC可否直接处理doGet?问个exception的问题
basic java question一个bug竟然是legacy code埋的地雷。
面试的一些题目HashMap一问
进入Java版参与讨论
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".:)

1 (共1页)
进入Java版参与讨论
相关主题
static getInstance()看到一个关于singleton的面试题
请教一个动态cast的问题SpringMVC可否直接处理doGet?
java 依赖注入和反射是必须掌握的吗?basic java question
java gc之前有没有办法得到notification?面试的一些题目
thread safe Singleton 的几种方法?问个exception的问题
一个想不明白的编码问题一个bug竟然是legacy code埋的地雷。
请教个html显示utf8 string的问题 (转载)HashMap一问
请教汉字的utf-8 mapping (转载)NullPointerException 问题
相关话题的讨论汇总
话题: utf话题: charset话题: null话题: exception