Jjang
Jjang

Reputation: 11444

Return in a 'finally' statement

I'm trying to read ObjectOutputStream from a file and convert it to an arraylist. This whole thing is happening inside a method which should read the file and return the array list:

public static List<Building> readFromDatabase(){
    String fileName="database.txt";
    FileInputStream fileIStream=null;
    ObjectInputStream in=null;
    List<Building> buildingsArr=null;
    try
     {
        fileIStream = new FileInputStream(fileName);
        in = new ObjectInputStream(fileIStream);
        buildingsArr=(ArrayList<Building>)in.readObject();
     }
     catch(IOException e)
     {
        e.printStackTrace();
     }
     catch(ClassNotFoundException e)
     {
        Console.printPrompt("ArrayList<Building> class not found.");
        e.printStackTrace();
     }
    finally{
        Console.printPrompt("Closing file...");
        close(in);
        close(fileIStream);
        return buildingsArr;
    }
}

Java tells me that this is dangerous. What are the alternatives? I can't put the return in the "try" block because it won't do it / it won't close files in the "finally" block. I need to both make sure files will be closed, and return the array list I created as well. Any ideas?

Upvotes: 2

Views: 283

Answers (3)

user177800
user177800

Reputation:

You must either throw an Exception or return a value:

All you need to prove this is comment out the return "File Not Found" after the finally block and see that it won't compile.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class ReturnFinallyExample
{
    public static void main(final String[] args)
    {
        returnFinally();
    }

    private static String returnFinally()
    {
        try
        {
            final File f = new File("that_does_not_exist!");
            final FileInputStream fis = new FileInputStream(f);
            return "File Found!";
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            System.out.println("finally!");
        }
        return "File Not Found!";
    }
}

You must have the return after the finally or you have to either:

declare the method to throws FileNotFoundExceptoin and re-throw the FileNotException out.

or

wrap the FileNotFoundException with throw new RuntimeException(e)

Upvotes: 0

atomman
atomman

Reputation: 2505

I would suggest starting to use Java 7, and the try with resources clause. http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Ex:

static String readFirstLineFromFile(String path) throws IOException {
    try (BufferedReader br = new BufferedReader(new FileReader(path))) {
        return br.readLine();
    }
}

Upvotes: 1

PermGenError
PermGenError

Reputation: 46428

I can't put the return in the "try" block because it won't do it / it won't close files in the "finally" block.

Wrong, finally block would still execute if you put return in try block. Thus you can return in your try block.

try
     {
        //your code
        return buildingsArr;
     }
     catch(IOException e)
     {
        e.printStackTrace();
     }
     catch(ClassNotFoundException e)
     {
        Console.printPrompt("ArrayList<Building> class not found.");
        e.printStackTrace();
     }
    finally{
        Console.printPrompt("Closing file...");
        close(in);
        close(fileIStream);
    }

Upvotes: 10

Related Questions