萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 587|回复: 0

1-1-3-1-11 try中初始化类

[复制链接]
发表于 2003-2-4 22:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

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]
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网 |网站地图

GMT+2, 2024-5-21 19:26 , Processed in 0.053616 second(s), 19 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表