【发布时间】:2011-04-21 19:11:18
【问题描述】:
我正在创建一个类——只是一个类,没有 main(),我在这一行收到“未报告的异常 java.io.FileNotFoundException; 必须被捕获或声明为抛出”的错误:
FileOutputStream outStr = new FileOutputStream(FILE, true);
我不明白;我放入了一个 try{} catch{} 块,它仍然报告错误。
此外,它还报告了 try 和两个 catch 行的“非法类型开始”,它还说 ';'预计两条捕获线。
我正在使用 NetBean IDE,仅供参考。
感谢您的帮助。
这里是完整的代码:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.FileNotFoundException;
public class UseLoggingOutputStream
{
String FILE = "c:\\system.txt";
try
{
FileOutputStream outStr = new FileOutputStream(FILE, true);
}
catch(FileNotFoundException fnfe)
{
System.out.println(fnfe.getMessage());
}
catch(IOException ioe)
{
System.out.println(ioe.getMessage());
}
}
【问题讨论】:
-
这一行是什么:
Logger.getLogger("outLog"), Level.ERROR)));?先修好。 -
你不需要在代码中最明显的地方放那么多
//Begin //Endcmets。他们制造的噪音比提供信息的要多。 -
已修复。我以为我为了这个问题/答案帖子的目的而删除了该行,但我表面上错过了它。我删除了 //Begin 和 //End cmets。谢谢。
标签: java exception try-catch filenotfoundexception