Can I set Eclipse to ignore “Unhandled exception type”

余生颓废 提交于 2019-12-19 19:01:07

问题


Is it possible to get Eclipse to ignore the error "Unhandled exception type"?

In my specific case, the reason being that I have already checked if the file exists. Thus I see no reason to put in a try catch statement.

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

Or am I missing something?


回答1:


Is it possible to get Eclipse to ignore the error "Unhandled exception type FileNotFoundException".

No. That would be invalid Java, and Eclipse doesn't let you change the rules of the language. (You can sometimes try to run code which doesn't compile, but it's not going to do what you want. You'll find that UnresolvedCompilationError is thrown when execution reaches the invalid code.)

Also note that just because the file existed when you called file.exists() doesn't mean that it still exists when you try to open it a tiny bit later. It could have been deleted in the meantime.

What you could do is write your own method to open a file and throw an unchecked exception if the file doesn't exist (because you're so confident that it does):

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);
    }
}

Note that "unchecked" here doesn't mean "there's no checking" - it just means that the only exceptions thrown will be unchecked exceptions. If you'd find a different name more useful, then go for it :)




回答2:


Declare that it throws Exception Or put it in a try finally bolok




回答3:


Here it is sir:

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();
}



回答4:


You cannot ignore that as it is not due to Eclipse, it is a compiler error, your code will not compile without your calls being enclosed in a try/catch clause. You can, however, leave the catch block empty to ignore the error although it is not recommended...



来源:https://stackoverflow.com/questions/22092794/can-i-set-eclipse-to-ignore-unhandled-exception-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!