您好,提问者:
IOException是IO异常。
filenotfoundexception是文件找不到路径或不存在异常。
他们都是Exception的子类。
import java.io.*;
public class FileDemo{
public static void main(String[] args){
try{
File file = new File("DDD:\\");//这里就会报FileNotFoundeException
}catch(FileNotFoundException e){
System.err.println("没有找到文件异常!");
}
FileWriter fw = null;
try{
fw = new FileWriter(file);
fw.write("xxxx");
}catch(IOException e){
System.err.println("创建文件或写入文件失败");
}finally{ //关闭文件总要执行的,但是关闭文件也是一个异常
try{
if(fw != null)
fw.close();
}.....
}
}
}
也可以合成一个异常,例如一下代码:
FileWriter fw= null;
try{
File file = new File("DD:\\"); //出现异常走FileNotFoundException
fw = new FileWriter(file); //出现异常走IOException
fw.write("xxx");
}catch(FileNotFoundException e){
}catch(IOException e){
}