1
我有這2種方法來從文件中讀取一些整數並將它們插入樹中。如果找到該文件,它工作正常,但如果找不到該文件,則不會打印「文件未找到」。爲什麼它沒有進入捕獲聲明?謝謝!嘗試捕獲錯誤
public static void openF(Tree myT)
{
try
{
x=new Scanner(new File("Number.txt"));
readF(myT);
}
catch(Exception e)
{
System.out.println("File not found");
}
}
// to read from the file
public static void readF(Tree myT)
{
while(x.hasNext()) //keeps going till it reaches the end of file
{
int a =x.nextInt();
myT.insert(a); //insert in tree
}
}
如果將空指針傳遞給它,文件的構造函數只會拋出異常。如果文件不存在,則不會拋出異常。掃描器構造函數是否拋出任何異常? readF方法沒有 – EdH 2013-05-12 10:48:37