dwwilson66
dwwilson66

Reputation: 7076

JAVA: Why does my return error during compile?

I inherited some code that's erroring out on me when I compile. I get one of two errors: One, that the return statement is missing, or another that I have a mismatched throw/try/catch. This error seems to be related to the position of return statement, which I've been moving around all morning. Its current position yields error: missing return statement. Either of the returns that are commented out yield a message that I must declare a try or catch with my throw.

Consider:

static private List<File> getFileListingNoSort(File aDirectory) throws FileNotFoundException {

try {
    List<File> result = new ArrayList<File>();
    File[] filesAndDirs = aDirectory.listFiles();
    List<File> filesDirs = Arrays.asList(filesAndDirs);
    for(File file : filesDirs) {
         result.add(file); //always add, even if directory
         if ( ! file.isFile() ) {
              List<File> deeperList = getFileListingNoSort(file);
              result.addAll(deeperList);
           // return result;
         } //close if
      // return result;
     } // close for
     } // close try for getFileListingNoSort
   // return result;
 catch (Exception exp) {
      System.err.println("Error: " + exp.getMessage());
 } //close catch getFileListingNoSort
 return result;
} //close method

I would think that the return would go after the try, but before the catch. However, that didn't work for me. I'm not really sure WHY I'm getting the areas, other than a misunderstanding of how the program is flowing with the try/catch. Can someone tell me the most appropriate place for the return and explain why?

Upvotes: 0

Views: 100

Answers (5)

pcalcao
pcalcao

Reputation: 15990

Your method has to return ALWAYS, either normally, or by issuing an Exception.

In case your catch clause doesn't throw an Exception, you need a return statement either as the last statement of the catch, or after the catch.

When you want to return something outside your try block, it's value must be defined before you go in the try.

For instance, if you want to return some value in the try (in case all goes well), and null if you get an Exception, your code would look something like this:

try{
   ... do loads of stuff
   return value;
} catch(Exception e) {
    e.printStackTrace(); //log it, whatever
    return null;
}

Upvotes: 3

nkukhar
nkukhar

Reputation: 2025

First of all your trying to access result variable that waht declared in try block and will not be visible out of it.

Upvotes: 1

İsmet Alkan
İsmet Alkan

Reputation: 5447

result is in try block, and it could be not-initialized. Taking it outside of the try block will solve your problem.

Upvotes: 1

Perception
Perception

Reputation: 80623

You are defining result inside your try block, but (trying to) return it outside of the block. This won't work because the variable is then out of scope. The simplest way to fix this is to declare the result variable outside of the try block.

Upvotes: 2

jlordo
jlordo

Reputation: 37823

Instead of

try {
    List<File> result = new ArrayList<File>();

do

List<File> result = new ArrayList<File>();
try {

this way the variable result will not be out of scope after the try/catch block.

The list will contain every element added up to until the exception occurred.

Upvotes: 4

Related Questions