Sepulchritude
Sepulchritude

Reputation: 183

Checked exceptions and File.exists() in Java

I have primarily a C# background (and am very much a newbie) so forgive me if my assumptions based on this are the problem.

Simply put, one of the features in a piece of software I'm working on (in Java) has the user input a file name. What I intend to do is have the program loop and append a string to the end of the file name from an array of possible append strings, to see if a file exists, and if it does, open it. I am guaranteed to have only ONE file of a given name, so breaking out of the loop on the first success isn't a bug (if the file name the user specifies is "foo" and the appendStrings array has "bar" and "baz" inside of it, I am guaranteed there will never be both a "foobar" and "foobaz" in the directory). What I eventually came up with was similar to this:

public FileReader LocateFile(String fileName)
{
    FileReader toReturn = null;
    for(int i = 0; i < appendStrings.length; i++)
    {
        File locatedFile = new File(fileName + appendStrings[i]);
        try
        {
            toReturn = new FileReader(locatedFile);
        }
        catch(FileNotFoundException ex)
        {
            continue;
        }
    }
    //...handling in case I didn't find a file.
}

Great, it works just fine. Except for two problems:

  1. The compiler is upset with me that I'm declaring a variable (ex) that I'm not using. I suppose I could log the exception or something, but that seems ridiculous because this will happen often and I'd rather not fill my log file with excessive noise just to make the compiler happy.
  2. I've read from countless sources that you don't use exceptions to control the flow of the program; you use them for exceptional circumstances.

My question is: is there a way to please the compiler in this situation? I have to catch that FileNotFoundException, so using File.exists() won't really solve my problem. Am I doing things backwards, or is this just how Java rolls?

Upvotes: 3

Views: 9513

Answers (3)

Grodriguez
Grodriguez

Reputation: 22025

I've read from countless sources that you don't use exceptions to control the flow of the program; you use them for exceptional circumstances.

You should be testing whether the file exists with File.exists() instead of using the exception for this purpose. You are right in that you still need to catch the exception when creating the FileReader object, however as pointed out by other people, a file may exist and you still may not be able to read it with a FileReader. These two situations must be accounted for separately.

The compiler is upset with me that I'm declaring a variable (ex) that I'm not using. I suppose I could log the exception or something, but that seems ridiculous because this will happen often and I'd rather not fill my log file with excessive noise just to make the compiler happy.

Indeed. However as said above, you should test with File.exists() first, if the file seems to exist yet you cannot create the FileReader, then that is a problem you should handle.

By the way, neither my compiler nor my IDE are warning about this unused variable. Maybe a setting in your IDE?

Upvotes: 1

Ankit Bansal
Ankit Bansal

Reputation: 5082

You are right, you shouldn't use exceptions to control your program flow.

You have already written the algo in plane strings just convert it into java code.

public FileReader LocateFile(String fileName)
{
    FileReader toReturn = null;
    for(int i = 0; i < appendStrings.length; i++)
    {
        File locatedFile = new File(fileName + appendStrings[i]);
         if(locatedFile.exists()) {
             toReturn = new FileReader(locatedFile);
             break;
        }
    }
    //...handling in case I didn't find a file.
}

Upvotes: 4

Colin Hebert
Colin Hebert

Reputation: 93197

First thing you're right about exceptions, they should be only used on exceptional circumstances.

A second point, with your code the continue; isn't necessary. If an exception is thrown, then you continue looping, but what happen if no exceptions are thrown ?
Well, you continue looping.

And a third point, the compiler isn't upset because of the ex variable, it's only your IDE which tells you that the variable isn't used, no worries it happens a lot.

Upvotes: 0

Related Questions