public class Try_catch1 
{
public static void main(String[] args)
{
DataInputStream f=null;
try
{
f=new DataInputStream(new FileInputStream("c:/file.txt"));
String s=f.readLine();
int a=Integer.parseInt(s);
String s1=f.readLine();
int b=Integer.parseInt(s1);
System.out.println(a+b);
}
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
}
catch(IOException e)
{
System.out.println(e.getMessage());
}
catch(NumberFormatException e)
{
System.out.println(e.getMessage());
}
finally
{
if(f==null)
System.out.println("数据流没打开");
f.close();//在此处系统报错,我不清楚什么原因,望赐教 System.out.println("关闭数据流");
}
}
}

解决方案 »

  1.   

    应该放在try{}里面,他有可能出异常,而且还应该放在else里面
    finally 

    if(f==null) 
    System.out.println("数据流没打开");  else{
    try {
    f.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }//在此处系统报错,我不清楚什么原因,望赐教 
    System.out.println("关闭数据流"); 
    }
      

  2.   

    因为f.close()让然可能抛出异常,可以再try……catch一下
    或者让main声明throws IOException
      

  3.   

    close()方法也可能产生异常,放在try{}里面就好了
      

  4.   

    没有捕获异常
    try {
    f.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
      

  5.   

    在使用close()的时候也会抛出异常,用try catch捕获,这样就好了
    try {
                    f.close();
                } catch (IOException e) {
                    
                    e.printStackTrace();
                }