2015-02-11 162 views
-3

我正在檢查文件夾和文件的目錄,然後更新最終顯示它們的方法。我無法使try catch塊正常工作。根據作業的指示需要。當輸入正確的文件路徑時,信息按預期顯示。當輸入不正確的路徑時,我沒有按預期得到JOptionPane。我是否使用正確的語法來查找不良路徑?是我如何設置try catch塊或者都是錯誤的?不會拋出異常

public void viewDirectoryContents(String basePath) { 
    try { 
// new file path 
     File file = new File(basePath); 
// basePath sent to updateDirectory() for display 
     updateDirectory(file); 
     if (!new File(basePath).exists()) 
      ; 
     { 
      throw new FileNotFoundException("The path " + basePath 
        + " does not exist or the cancel button was clicked"); 
     } 
    } catch (FileNotFoundException e) { 
     JOptionPane.showMessageDialog(null, e); 
    } 
} 
+1

如果您使用更明智的格式化你的錯誤會更明顯。 – 2015-02-11 16:38:48

+0

你的'catch'塊是多餘的,只是在你的'if'裏面處理「文件不存在」。我假設你想檢查'!file.exists()'而不是'!new File(basePath).exists()' – 2015-02-11 16:45:14

回答

7

刪除被終止分號if語句

if (!new File(basePath).exists()); 
           ^
+0

它沒有區別 – user3137110 2015-02-11 16:42:56

+0

如果沒有對話框出現,那麼文件必須實際存在於在'basePath'中定義的位置 – Reimeus 2015-02-11 17:11:06