2016-01-26 38 views
0

自从我使用Java以来​​,我一直在使用Java,所以我觉得这很让人困惑,但我在'FileProcessor.java'文件中有一个'FileProcessor'类。我想在我的“Driver.java”文件中使用它,但我不断收到此错误:在不同的文件中处理FileNotFoundException

error: unreported exception FileNotFoundException; must be caught or declared to be thrown

我在Java的整个例外事情有点困惑,我想我处理得在我的FileProcessor.java文件中,但我不知道。

FileProcessor.java

import java.io.File; 
import java.io.BufferedReader; 
import java.io.FileReader; 
import java.io.IOException; 
import java.io.FileNotFoundException; 

public class FileProcessor{ 
    /** 
    * Reads one line from file 
    * @param arg A file 
    * @return The line that is read 
    */ 
    public String readLineFromFile(File f) throws FileNotFoundException, 
               IOException{ 
     BufferedReader br = null; 
     String line; 
     try{ 
     br = new BufferedReader(new FileReader(f)); 
     line = br.readLine(); 

     return line; 
     } 
     catch(IOException e){ 
     e.printStackTrace(); 
     System.err.println("Read method failed"); 
     throw new IOException(); 
     } 
     catch(FileNotFoundException e1){ 
     e1.getMessage(); 
     System.err.println("File is not found"); 
     throw new FileNotFoundException(); 
     } 

    } 

} 

Driver.java

import java.io.File; 
import java.io.FileNotFoundException; 

public class Driver{ 
    public static void main(String args[]){ 
     File inFile = null; 
     if (0 < args.length){ 
     inFile = new File(args[0]); 
     } 
     else{ 
     System.err.println("No input file found"); 
     System.exit(0); 
     } 
     FileProcessor fileProcessor = new FileProcessor(); 
     String lineRead; 
     try{ 
     lineRead = fileProcessor.readLineFromFile(inFile); 
     } 
     catch(FileNotFoundException e){ 
     throw new FileNotFoundException(); 
     } 
    } 
} 
+1

为什么你要捕捉异常,只是为了直接在后面抛出一个新的(完全不相关的,没有帮助的信息)异常。你应该看看一些关于异常处理的教程。 “如果你不能处理它,那就不要” –

回答

0

main抛出在catch块不能的主要外部钓到了一条新FileNotFoundException。更改:

import java.io.File; 
import java.io.FileNotFoundException; 

public class Driver{ 
    public static void main(String args[]){ 
     File inFile = null; 
     if (0 < args.length){ 
     inFile = new File(args[0]); 
     } 
     else{ 
     System.err.println("No input file found"); 
     System.exit(0); 
     } 
     FileProcessor fileProcessor = new FileProcessor(); 
     String lineRead; 
     try{ 
     lineRead = fileProcessor.readLineFromFile(inFile); 
     } 
     catch(FileNotFoundException e){ 
     System.out.print(e.getMessage()); 
     } 
    } 
} 
0

有关异常的事情很少。

  1. 您捕获异常和你写来处理异常,例记录你需要适当的代码,设置错误消息或触发失败的邮件,用新的文件名等重试对于这个你尝试一起写catch块。

2.您捕获异常,并编写处理Exception,Example logging等所需的适当代码,但您希望将来的代码调用您的方法来捕获并再次处理异常。在这种情况下,您将使用throw new重新抛出异常并将其添加到throws中。甚至可以抛出新的异常类型等

catch(NullPointerException e) { 
    throw new RecipeNotFoundException("No recipe found"); 
} 
  • 延迟的处理,以将来代码调用此方法。这是通过写入throws子句完成的。抛出告诉方法调用一个方法,在这里有可能发生异常,并且它没有被捕获,并且你需要捕获并且你不会写catch。
  • 在您的代码中,您已经捕获了FileProcessor.readLineFromFile(File)方法中的代码,但您也已经为方法添加了throws子句。所以系统认为有可能抛出一个异常,并且没有可能从catch块中捕获另一个FileNotFoundException异常。

    捕捉异常之后还有一件事你已经抛出了相同的异常抛出新的IOException();并抛出新的FileNotFoundException();也删除。

    如果在此处查看FileNotFoundException和IOException的Java文档。 docs.oracle.com/javase/7/docs/api/java/io/...和docs.oracle.com/javase/7/docs/api/java/io/IOException.html你会注意到FileNotFoundException实际上会扩展IOException,所以你不必实际捕获FileNotFoundException。

    +0

    因此,删除thride子句INSIDE Driver.java给了我一个错误'unreported exception IOException',我用'catch(IOException e1)'修复了这个错误,但是我不' t得到为什么我必须在我的FileProcessor类和Driver类中捕获它们。如果我删除FileProcessor.java中的引发,我会得到'缺少返回语句'错误 – Homerdough

    +0

    如果您在此处查看FileNotFoundException和IOException上的Java文档。 https://docs.oracle.com/javase/7/docs/api/java/io/FileNotFoundException.html 和 https://docs.oracle.com/javase/7/docs/api/java/io /IOException.html 您会注意到FileNotFoundException实际上会扩展IOException,因此您不必实际捕获FileNotFoundException。 –

    +0

    如果您删除FileProcessor内部的引发,则会产生编译错误,因为您的方法需要返回String值,并且只有在没有异常(即try块)时才返回String。你的catch块不会返回任何东西。从catch块返回一些字符串,或者你在函数的末尾返回一些String(即,在try和catch之后) –

    0

    当你捕获它们时抛出一个新的IO-或FileNotFoundException不能很好地处理这个异常。

    catch(IOException e){ 
        e.printStackTrace(); 
        System.err.println("Read method failed"); 
        throw new IOException(); 
    } 
    

    首先,你松开了异常信息(哪个文件找不到,到底发生了什么......)。其次,如果你再次扔掉它,它不会真的抓住它们,所以你必须再次捕捉到它们。

    所以,最简单的可能的解决方案是删除throw语句。

    public class FileProcessor{ 
        public String readLineFromFile(File f) 
         // this can be deleted if you catch the exceptions 
         // in here (and do not rethrow them) 
         // throws FileNotFoundException, IOException 
        { 
         BufferedReader br = null; 
         try { 
         br = new BufferedReader(new FileReader(f)); 
         return br.readLine(); 
         } catch(IOException e) { 
         e.printStackTrace(); 
         // throw new IOException(); 
         } 
         // this can be deleted because FileNotFoundException is a 
         // subclass of IOException and is caught above 
         // catch(FileNotFoundException e1){ 
         // e1.getMessage(); 
         // System.err.println("File is not found"); 
         // throw new FileNotFoundException(); 
         // } 
    
         // and after all, you should close the BufferedReader 
         // or use the "try-with-resources" 
         finally { 
         if(br != null) { br.close(); } 
         } 
        } 
    } 
    
    相关问题