日期:2014-05-20  浏览次数:20849 次

java新手求问
程序代码:
import java.io.*;

class Test4{
  public static void main(String args[])throws IOException{
  int i;
  FileInputStream fin;
  try{
  fin=new FileInputStream(args[0]);
  }catch(FileNotFoundException e){
  System.out.println("File not be founded!");
  return;
  }catch(ArrayIndexOutOfBoundsException e){
  System.out.println("Usage:showFile File");
  return;
  }
   
  do{
  i=fin.read();
  if(i!=-1){System.out.print((char)i);}
  }while(i!=-1);

  fin.close();
 }//main
}//Test4
   

为什么会产生错误呢?
:\java>javac Test4.java
Test4.java:4: 不兼容的类型
找到: IOException
需要: java.lang.Throwable
  public static void main(String args[])throws IOException{
  ^
.\IOException.java:15: 不兼容的类型
找到: IOException
需要: java.lang.Throwable
  catch(IOException e){
  ^
.\IOException.java:39: 不兼容的类型
找到: IOException
需要: java.lang.Throwable
  catch(IOException e){

------解决方案--------------------
探讨
throws IOException
去掉呢
建议你还是用IDE吧 这样 哪出错就知道了
你这可能是异常的问题

------解决方案--------------------
你自己在main函数里面try-catch了,哪有ioexception抛出呢?
解决方法:
第一种方法:把main函数里面的try-catch去掉
第二种方法:把main函数后面的throws IOException去掉

另外,main是允许抛出异常的,任何函数都允许