當我試圖用我的測試代碼中的文件實例化一個對象時,我遇到未報告的FileNotFoundException的錯誤。我正在使用/創建的類在構造函數中只有一個FileNotFoundException(只有一個構造函數),所以我不太確定爲什麼在聲明對象時要求額外的FileNotFound。FileNotFoundException未報告
//Constructor
public readFile(File file)throws FileNotFoundException {
//do i need to create a file object here?
Scanner inScanFile = new Scanner(file);
}
///////////Running Code from JUNIT below//////////////
public void Empty(){
File testFile = new File("HARRY_POTTER_TRIVIA.txt");
ReadingClass newReadtest = new ReadingClass(testFile); //Error occurs here
嗯......「File Not Found」的哪部分你不明白? – JohnG
'Scanner inScanFile = new Scanner(file);'試圖在絕對路徑中查找文件,但該文件不存在,因此會引發異常。 –
如果類中存在FileNotFoundException,爲什麼當我使用該類實例化一個對象時,我得到一個未報告的異常以包含額外的FileNotFoundException。 – samgrey