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("关闭数据流");
}
}
}
{
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("关闭数据流");
}
}
}
finally
{
if(f==null)
System.out.println("数据流没打开"); else{
try {
f.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//在此处系统报错,我不清楚什么原因,望赐教
System.out.println("关闭数据流");
}
}
或者让main声明throws IOException
try {
f.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
f.close();
} catch (IOException e) {
e.printStackTrace();
}