Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I set Eclipse to ignore "Unhandled exception type"

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?

like image 739
ChaosPixy Avatar asked Feb 28 '14 10:02

ChaosPixy


People also ask

How do you deal with unhandled exception in Java?

The try-catch is the simplest method of handling exceptions. Put the code you want to run in the try block, and any Java exceptions that the code throws are caught by one or more catch blocks. This method will catch any type of Java exceptions that get thrown. This is the simplest mechanism for handling exceptions.

What is unhandled exception type?

An exception is a known type of error. An unhandled exception occurs when the application code does not properly handle exceptions. For example, When you try to open a file on disk, it is a common problem for the file to not exist.

What happens to an unhandled exception?

An unhandled exception is an exception that does not have an associated handler. In C++ any unhandled exception terminates the program. It is unspecified whether the stack is unwound in this case, i.e. destructors of successfully constructed local variables may be executed or not depending on the compiler.


3 Answers

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 :)

like image 59
Jon Skeet Avatar answered Oct 14 '22 19:10

Jon Skeet


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

like image 20
pizzaEatingGuy Avatar answered Oct 14 '22 21:10

pizzaEatingGuy


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();
}
like image 27
Manitoba Avatar answered Oct 14 '22 19:10

Manitoba