![](/img/trans.png)
[英]I keep getting “FileNotFoundException” even though I handled the exception and the file does exist.
[英]Getting FileNotFoundException even though I declared it to be thrown
我目前正在使用鏈接列表編寫文本編輯器,我已經完成了很多但是在嘗試測試程序的命令行時遇到了FileNotFoundException,即使我聲明它被拋出。
這是我的編輯器的骨架:
public class Editor {
public Editor() {
}
public void commandLine() throws FileNotFoundException {
}
}
這是我的程序的驅動程序:
public class EditorTest
{
public static void main(String[] args)
{
Editor asdf = new Editor();
asdf.commandLine();
}
}
我仍然收到一個未報告的FileNotFoundException錯誤,即使我聲明它在我的命令行方法中拋出。 怎么了?
您需要向main
方法添加throws FileNotFoundException
。 或者,您可以添加:
try {
asdf.commandLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
根據您的main
方法,取決於您根據該異常需要做什么。
喲也需要在主要上聲明它
public static void main(String[] args) throws FileNotFoundException {
聲明在方法中拋出異常(即使用throws MyException )並不會阻止拋出異常,而是允許方法拋出該方法的調用者必須捕獲該異常
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.