|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
发信人: tangfeng (唐风), 信区: Java
标 题: Re: 如果在try 里面对一个类初始化,出了问题
发信站: BBS 水木清华站 (Sat Jul 13 23:37:15 2002)
那样也不行
如下:
FileInputStream in;
try{ //open file for input
in=new FileInputStream(args[0]);
}catch(FileNotFoundException e){
System.out.println("can't open the file");
System.exit(-1);
}
in.read(s1);
出错还是说 variable in might not have been initialized.
【 在 zms (来福) 的大作中提到: 】
: 什么意思
: 是不是这样:
: try {
: String s="abc";
: } catch (Exception e) {
: System.exit(1);
: }
: System.out.println(s);
: 你应该改成这样:
: String s;
: try {
: ...................
--
※ 来源:·BBS 水木清华站 smth.edu.cn·[FROM: 166.111.169.239]
发信人: zdp (乱我心者~短发), 信区: Java
标 题: Re: 如果在try 里面对一个类初始化,出了问题
发信站: BBS 水木清华站 (Sun Jul 14 00:05:43 2002)
应该是这样,在你的in.read(s1)出,编译器发现in这个对象在前面可能既没有new,
也没有赋值,所以认为可能没有初始化。
之所以说“可能”,因为try catch中的语句可能执行不到。
最好养成这样的习惯:FileInputStream in=null;
【 在 tangfeng (唐风) 的大作中提到: 】
: 那样也不行
: 如下:
: FileInputStream in;
: try{ //open file for input
: in=new FileInputStream(args[0]);
: }catch(FileNotFoundException e){
: System.out.println("can't open the file");
: System.exit(-1);
: }
: in.read(s1);
: 出错还是说 variable in might not have been initialized.
: ...................
--
※ 来源:·BBS 水木清华站 smth.edu.cn·[FROM: 166.111.178.147]
发信人: qyjohn (Sweet Potato -- 太阳最红,咪咪最亲), 信区: Java
标 题: Re: 如果在try 里面对一个类初始化,出了问题
发信站: BBS 水木清华站 (Sun Jul 14 00:06:25 2002)
在try & catch里面初始化的变量和对象仅在这个try & catch里面
是保证有效的。为了安全起见,如果你的变量或者是对想仅在try
& catch里面初始化,那么在try & catch之外使用这些变量和对象
的时候编译器会告诉你这有可能导致错误。事实上,这经常会导致
错误。
【 在 tangfeng (唐风) 的大作中提到: 】
: 那样也不行
: 如下:
: FileInputStream in;
: try{ //open file for input
: in=new FileInputStream(args[0]);
: }catch(FileNotFoundException e){
: System.out.println("can't open the file");
: System.exit(-1);
: }
: in.read(s1);
: 出错还是说 variable in might not have been initialized.
: ...................
--
("`-''-/").___..--''"`-._ 云与清风常拥有,
`6_ 6 ) `-. ( ).`-.__.`) 冰雪知音世难求。
(_Y_.)' ._ ) `._ `. ``-..- 击节纵歌相对笑,
_..`--'_..-_/ /--'_.' ,' 案上诗书杯中酒。
(il),-'' (li),' ((!.-' 2000.12.31
___________________________________________________________________
※ 来源:·BBS 水木清华站 smth.edu.cn·[FROM: 64.166.188.154] |
|