程序代码:
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){
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){
去掉呢
建议你还是用IDE吧 这样 哪出错就知道了
你这可能是异常的问题
嗯,把throws IOException去掉,改用eclipse编程
提醒LZ一下,结贴是一种美德
解决方法:
第一种方法:把main函数里面的try-catch去掉
第二种方法:把main函数后面的throws IOException去掉另外,main是允许抛出异常的,任何函数都允许
ArrayIndexOutOfBoundsException e运行时异常,不用捕捉,程序写 的 健壮些 就ok啦
G:\java>javac Test4.java
Test4.java:19: 未报告的异常 java.io.IOException;必须对其进行捕捉或声明以便抛出
i=fin.read();
^
Test4.java:23: 未报告的异常 java.io.IOException;必须对其进行捕捉或声明以便抛出
fin.close();
^
为什么?
catch 他