package test;import java.io.*;public class asd {
static FileInputStream in = new FileInputStream("c:\\Example1.xml");
public static void main(String[] args) throws IOException{
int b = 0;
while((b=in.read())!=-1){
System.out.print((char)b);}
}
}这段代码中为什么static FileInputStream in = new FileInputStream("c:\\Example1.xml");这里会报错咧?说我未处理异常类型,可是我在后面加上throws Exception的话 throws还是报错 说“应为,”这我就搞不懂了

解决方案 »

  1.   

    try{
      FileInputStream in = new FileInputStream("c:\\Example1.xml"); 
    }catch(Exception e){}
    试一试你只是对方法 main()抛出了异常,没有具体对某些会抛出异常的语句做出异常捕获
      

  2.   

    static FileInputStream in = new FileInputStream("c:\\Example1.xml"); 定义的是静态的对象,在读取文件时没有办法捕获异常啊,在定义属性时是没有办法直接使用try...catch...以及throws的,只有在相应的方法中才可以的啊
    可以这样修改:public class Test120090402 {
    static FileInputStream in; public static void main(String[] args) throws IOException {
    in = new FileInputStream("c:\\Example1.xml");
    int b = 0;
    while ((b = in.read()) != -1) {
    System.out.print((char) b);
    }
    }
    }
      

  3.   

    static静态变量在类初始化的时候被调用,并不是在main中被调用。
      

  4.   


    in 的声明不能放在try/catch中,这样就缩小了他的作用域
    在main方法中是不能访问的,应该把in的声明放在外面,
    而他的实例化过程应置于一个{}中
    public class Test { 
    static FileInputStream in = null;
    {
    try {
     in = new FileInputStream("c:\\Example1.xml"); 
    } catch (Exception e) {}
    } public static void main(String[] args) throws IOException{ 
    int b = 0; 
    while((b=in.read())!=-1){ 
    System.out.print((char)b);} 


    }