JAVA I/O处理中throws Exception的问题

ltelink 2012-06-15 02:36:14
在学习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,谢谢。
...全文
842 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lost_guy_in_scut 2012-06-26
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

那么将下划线部分代码更改为
public static void main(String[] args) throws Exception
运行程序似乎没有影响。
请问这个又有什么变化呢?
[/Quote]
Exception 是IOException的父类,相当于Exception e = new IOExcepiton(); 所以说是没影响的。
ltelink 2012-06-26
  • 打赏
  • 举报
回复
那么将下划线部分代码更改为
public static void main(String[] args) throws Exception
运行程序似乎没有影响。
请问这个又有什么变化呢?
WinterFall 2012-06-15
  • 打赏
  • 举报
回复
因为可能会有IO异常,所以要抛出,就比如你写了个错误的文件地址给他,他找不到,如果没有抛出异常机制的话,是不是就是没有反应啊,没有反应你就急了,一直点运行运行运行,可还是不知道错误在哪里,有了提前抛出异常机制就能在真正出现异常时候给出异常,快速解决问题。


下面是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)
看着奢扣 2012-06-15
  • 打赏
  • 举报
回复
有些程序可能会引起异常,这样的异常一定要捕获,要么try catch 要么throws
淡定的峰哥 2012-06-15
  • 打赏
  • 举报
回复
像IOException 、FileNotFoundException等异常属于已检查异常,编写代码的时候遇到这些异常要么用try catch捕获处理,要么在方法上声明抛出,否则代码就会编译不通过。你这在main方法上throws IOException 就是此道理,已检查异常常见的还有操作JDBC的SQLExceotion,ClassNotFundException
还有一类异常像空指针、数组越界等属于运行时异常,可不用在代码中进行try catch或在方法声明时抛出,运行时异常一般属于程序bug,应该避免出现这类异常
Sam_qifa 2012-06-15
  • 打赏
  • 举报
回复
程序里面有某些错误是一定得捕捉到并进行处理的。这些错误叫checked exception,比如你在使用网络通信、使用IO读取文件等等情况下是很有可能出现异常的(网络断了,文件半路被删了)。这时候你的程序总不能也跟着挂掉吧。所以编译器就要求你用try{}catch{}捕捉这些错误。当然,你也可以不进行处理,直接丢给上一层调用者。你用public static void main(String[] args) throws IOException 就是这种情况。如果你程序里面自己try{}catch{}处理了,那丢不丢就无所谓了。
jlu_lamp_lamp 2012-06-15
  • 打赏
  • 举报
回复
IO异常是强制性异常
就是避免异常,通常要把IO的部分代码添加异常处理
两种方法:
1.放在try,catch里面处理异常
2.在这一层不处理,用throws向上层抛出异常,让上层或以上的方法去处理
经过这两种方法后就等于处理过异常了,一些需要添加强制性异常的代码就可以通过编译了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧