2017-02-17 26 views
0

我目前正在處理一些異常處理,並在使用驅動程序類時遇到問題。驅動程序錯誤:「未報告的異常java.io.FileNotFoundException;必須被捕獲或聲明爲拋出。」我無法編輯驅動程序以將「拋出FileNotFoundException」添加到main。捕捉FileNotFoundException。在嘗試/ catch塊之前初始化掃描儀的問題

這是主程序的代碼片段。我知道我需要用try/catch來捕捉異常,但我無法弄清楚如何在try塊之前初始化掃描器。

public program(String file1, String file2) throws FileNotFoundException 
{ 
    File f1 = new File(file1); 
    File f2 = new File(file2); 

    try(Scanner scan = new Scanner(f1); Scanner scan2 = new Scanner(f2);) 
    { 
    } 
    catch(FileNotFoundException e){} 

    int a = scan.nextInt(); //THIS IS WHERE I RUN INTO PROBLEMS (scan not found) 
    scan.nextLine(); 
    int b = scan.nextInt(); 
} 

回答

0

我通過去除 「拋出FileNotFoundException異常」

public program(String file1, String file2) 
{ 
    try 
    { 
     File f1 = new File(file1); 
     File f2 = new File(file2); 
     int a = scan.nextInt(); //THIS IS WHERE I RUN INTO PROBLEMS (scan not found) 
     scan.nextLine(); 
     int b = scan.nextInt(); 
    } 
    catch(FileNotFoundException e){} 
} 
固定它