由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - what is your opinion in this case?
相关主题
basic java questionNullPointerException 问题
刚刚开始学习java,麻烦帮我看一下我哪里错了行吗?谢谢问一个java基础的初始化的问题,一直搞不明白 (转载)
anyone saw this on code?跪求大牛指点Java,看不懂什么意思。
thread safe Singleton 的几种方法?关于new operator的问题
这段程序的输出是什么? 为什么库里有一个函数名也叫Time
structure in Java??新手问题
面试的一些题目超级菜鸟问题:什末时候load class
[合集] TexPen questionEclipse 编译求助
相关话题的讨论汇总
话题: randomgen话题: static话题: myclass话题: numbers
进入Java版参与讨论
1 (共1页)
c******n
发帖数: 4965
1
/* generate a bunch of "random" numbers
taken in serial one by one from a list of numbers in
a given file
*/
class RandomGen(
public RandomGen() throws IOException {
InputStream ios = ..... open "rand.txt" in CLASSPATH;
//
read the file into memory,
}

int idx;
public Integer gen() {
return list_of_numbers[idx++];
}
}
something like the above.
the problem is that the constructor throws IOException, so if I use this
class, and most likely I use it as a static member var:
class MyClass {
static RandomGen gen = new RandomGen();
....
}
then I can't throw in the static initializer.
what is your strategy of handling this?
1) let ctor of RandomGen throw RuntimeException instead?
2) catch the IOException in static initializer of MyClass?
3) ???
Thanks
g*****g
发帖数: 34805
2
What's wrong with a static initialization block?
static {
catch your exception here if you wish
}

【在 c******n 的大作中提到】
: /* generate a bunch of "random" numbers
: taken in serial one by one from a list of numbers in
: a given file
: */
: class RandomGen(
: public RandomGen() throws IOException {
: InputStream ios = ..... open "rand.txt" in CLASSPATH;
: //
: read the file into memory,
: }

c******n
发帖数: 4965
3
sure I can do that,
it's just that , for example, if this is some error that prevents user from
going further (for example missing the random.txt source in our case), then
it means we'd better halt the code.
doing this catch in initialization block would need me
to do
System.exit(-1) to halt it,
while just percolating the RuntimeException would automatically halt it
without adding too much distraction to the code

【在 g*****g 的大作中提到】
: What's wrong with a static initialization block?
: static {
: catch your exception here if you wish
: }

1 (共1页)
进入Java版参与讨论
相关主题
Eclipse 编译求助这段程序的输出是什么? 为什么
BufferedWriter里的write()structure in Java??
关于char和int的问题面试的一些题目
问个面试题, 谢谢[合集] TexPen question
basic java questionNullPointerException 问题
刚刚开始学习java,麻烦帮我看一下我哪里错了行吗?谢谢问一个java基础的初始化的问题,一直搞不明白 (转载)
anyone saw this on code?跪求大牛指点Java,看不懂什么意思。
thread safe Singleton 的几种方法?关于new operator的问题
相关话题的讨论汇总
话题: randomgen话题: static话题: myclass话题: numbers