java throws IOException 问题.

import java.io.*;
public class Ex6_2 {

/**
* @param args
*/
public static void main(String[] args)throws IOException{
// TODO Auto-generated method stub
String fileName="D:/Hello.txt";
BufferedWriter out=new BufferedWriter(new FileWriter(fileName));
out.write("Hello!");
out.newLine();
out.write("This is another text file using BufferedWriter,");
out.newLine();
out.write("So I can use a common way to start a newline");
out.close();
}
}
其中throws IOException是什么意思啊? 为什么要加这个?

抛异常分为两种方式,一种是抛出异常就处理异常就是所谓的try{}catch(){},还有一种就是你写的那种回避异常,就是在出现异常时不处理,在调用此方法时处理该异常,什么时候调用,什么时候处理异常。
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-11-23
如果发生IO异常,例如文件不存在,不抛出异常,就要处理异常(try catch),而抛出是交给虚拟机处理。不做任何错事程序是要出错的
第2个回答  2011-11-23
out对象是流对象,流的执行过程会参数io异常,所以一般需要俘获处理这些异常。
相似回答