我可以将Eclipse设置为忽略“未处理的exception类型”

是否有可能让Eclipse忽略错误“未处理的exception类型”?

在我的具体情况下,原因是我已经检查过该文件是否存在。 因此,我认为没有理由提出try catch语句。

file = new File(filePath); if(file.exists()) { FileInputStream fileStream = openFileInput(filePath); if (fileStream != null) { 

或者我错过了什么?

是否有可能让Eclipse忽略错误“未处理的exception类型FileNotFoundException”。

不会。这将是无效的Java,Eclipse不允许您更改语言规则。 (您有时可以尝试运行不编译的代码,但它不会执行您想要的操作。当执行到达无效代码时,您会发现UnresolvedCompilationError被抛出。)

另请注意,仅仅因为调用file.exists()时文件存在并不意味着当您尝试稍后打开它时它仍然存在。 它可能在此期间被删除。

可以做的是编写自己的方法来打开文件,如果文件不存在则抛出未经检查的exception(因为您对它确实如此):

 public static FileInputStream openUnchecked(File file) { try { return new FileInputStream(file); } catch (FileNotFoundException e) { // Just wrap the exception in an unchecked one. throw new RuntimeException(e); } } 

请注意,“unchecked”在这里并不意味着“没有检查” – 它只是意味着抛出的唯一exception将是未经检查的exception。 如果你找到一个更有用的不同名称,那就去吧:)

声明它throws Exception或者把它放在一个尝试终于bolok

这是先生:

 try { file = new File(filePath); if(file.exists()) { FileInputStream fileStream = openFileInput(filePath); if (fileStream != null) { // Do your stuff here } } } catch (FileNotFoundException e) { // Uncomment to display error //e.printStackTrace(); } 

您不能忽略它,因为它不是由于Eclipse,它是一个编译器错误,如果没有将您的调用包含在try / catch子句中,您的代码将无法编译。 但是,您可以将catch块留空以忽略错误,尽管不建议…