在学习Java I/O处理和异常处理的过程中,按照课本(《Java程序设计完全自学手册》)的一个例子来写
package test;
import java.io.*;
public class TestWriter {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the reading directory:");
String path= bufferedReader.readLine();
System.out.println("Please write down the contents, then press ENTER # to end:");
BufferedWriter bufferedWriter =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
String s1=bufferedReader.readLine();
while(!s1.equals("#")){
bufferedWriter.write(s1);
s1=bufferedReader.readLine();
}
bufferedWriter.close();
}
}
代码中下划线部分为何需要抛出异常?在删除throws Exception之后,Eclipse分别提示出现如下错误:
Unhandled exception type IOException (红色字体部分)
Unhandled exception type FileNotFoundException (蓝色字体部分)自己通过baidu找不到合适的答案,请大家帮忙解释一下为何此处需要throws Exception,谢谢。
package test;
import java.io.*;
public class TestWriter {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Please enter the reading directory:");
String path= bufferedReader.readLine();
System.out.println("Please write down the contents, then press ENTER # to end:");
BufferedWriter bufferedWriter =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path)));
String s1=bufferedReader.readLine();
while(!s1.equals("#")){
bufferedWriter.write(s1);
s1=bufferedReader.readLine();
}
bufferedWriter.close();
}
}
代码中下划线部分为何需要抛出异常?在删除throws Exception之后,Eclipse分别提示出现如下错误:
Unhandled exception type IOException (红色字体部分)
Unhandled exception type FileNotFoundException (蓝色字体部分)自己通过baidu找不到合适的答案,请大家帮忙解释一下为何此处需要throws Exception,谢谢。
就是避免异常,通常要把IO的部分代码添加异常处理
两种方法:
1.放在try,catch里面处理异常
2.在这一层不处理,用throws向上层抛出异常,让上层或以上的方法去处理
经过这两种方法后就等于处理过异常了,一些需要添加强制性异常的代码就可以通过编译了
还有一类异常像空指针、数组越界等属于运行时异常,可不用在代码中进行try catch或在方法声明时抛出,运行时异常一般属于程序bug,应该避免出现这类异常
下面是oracle官方api,你在构造这个FileOutputStream的时候是要求抛出FileNotFoundException的,而你代码中Exception是他的父类,包含了所有的Exception,只要出错,也能抛出异常,具体不同的方法或者构造器会引起各种各样的Exception,所以我们偷懒的话可以一起在throws后面抛出Exception。public FileOutputStream(String name)
throws FileNotFoundException
Creates a file output stream to write to the file with the specified name. A new FileDescriptor object is created to represent this file connection.
First, if there is a security manager, its checkWrite method is called with name as its argument.If the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason then a FileNotFoundException is thrown.Parameters:
name - the system-dependent filename
Throws:
FileNotFoundException - if the file exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
SecurityException - if a security manager exists and its checkWrite method denies write access to the file.
See Also:
SecurityManager.checkWrite(java.lang.String)
public static void main(String[] args) throws Exception
运行程序似乎没有影响。
请问这个又有什么变化呢?