【发布时间】:2012-10-25 03:04:48
【问题描述】:
我想从这段代码中删除异常处理程序,因为我不明白它存在的原因,而且我相信它在我的代码阶段没有做任何事情。 我有这个:
public void ToFile(){
try{
PrintWriter pw = new PrintWriter(new FileWriter(file, false));
for (String st:stringarray){
pw.println(st);
pw.close();
}
}catch(Exception exception)
{}
}
当我删除异常部分时,我将其转换为这个,但它给了我错误...:
public void ToFile(){
PrintWriter pw = new PrintWriter();
for (String st:items){
pw.println(srtingarray);
pw.close();
}
}
我该怎么办? 编辑 错误: 没有找到适合 PrintWriter 的构造函数。
【问题讨论】:
-
处理异常是一个好习惯。为什么要删除该代码?
-
你得到什么错误?
-
"What should I do ? EDIT ERROR: no suitable constructor found for PrintWriter."我会查看 PrintWriter API 并仅使用适当的构造函数。您将需要阅读我正在考虑的 Java 教程中的异常教程。您还需要了解介绍/基础教程。 -
我看不出一个人多次调用 close() 方法的任何原因。任何时候
st有多个元素都会抛出异常。这是很奇怪的代码。 -
"because I dont understand exactly what exceptions do, I want to delete them."这有点像去找汽车修理工说:“我不知道如何使用我车上的刹车。你能把它们去掉吗?”答案是学习如何使用你的休息时间(例外)。请转至this link to the Java Tutorials 并搜索例外部分。