Java return type after try/catch

I want to return data of type String from my method i created. Eclipse says need to specifiy a return type after the try catch block... When I do that, Eclipse then tells me that I need to declare String data as a Local variable... What is going wrong here?

private String ReadData() {
    try {
        FileInputStream fis = null;
        InputStreamReader isr = null;
        String data = null;
        fis = KVOContact.this.openFileInput("data.txt");
        isr = new InputStreamReader(fis);
        char[] inputBuffer = new char[fis.available()];
        isr.read(inputBuffer);
        data = new String(inputBuffer);
        isr.close();
        fis.close();

    } catch (IOException ioe) {
        Log.e("KVOContact", "IOError" + ioe);
    }
    return data;
}

Answers


You're declaring data within the try block. It's out of scope outside that block.

You could just move the declaration to before the try block - but personally I think it would probably make more sense to remove the catch block entirely, and declare that the method can throw IOException. You should also close your FileInputStream and InputStreamReader in finally blocks so that you don't leave them open if an exception is thrown.


The data variable is limited to the scope of the try clause. Declare it outside the try instead.


You need to make some changes in your code:

private String ReadData() {
    String data = null;
    try {
        FileInputStream fis = null;
        InputStreamReader isr = null;        
        fis = KVOContact.this.openFileInput("data.txt");
        isr = new InputStreamReader(fis);
        char[] inputBuffer = new char[fis.available()];
        isr.read(inputBuffer);
        data = new String(inputBuffer);
        isr.close();
        fis.close();
    } catch (IOException ioe) {
        Log.e("KVOContact", "IOError" + ioe);
    }
    return data;
}

You need to declare a String data variable not inside the try block, because otherwise it wont be seen outside the try catch block.


Yes, you need to use return statement in catch part as well, study the following code,

private String ReadData() 
{
     try
     {
     .
     .
     . 
     return stringVariable;
     }
     catch ( IOException ioe )
     {
         Log.e("KVOContact", "IOError" + ioe);
         return NULL;
     }
}

Place the return data statement inside the try block itself.

The catch will be called only when some error occurrs inside the try block otherwise the try gets executed and the call is returned to where it originated from.


Need Your Help

Start two Android emulators with different locale

android android-emulator locale emulation qemu

Most of the idea is in the title, I just would like to know if it is possible to start 2 emulators at the same time with 2 different locale (to test the I18n of my app). If there was a way of doing...

Google web toolkit doesn't work with CachedRowSet

mysql gwt cachedrowset

I'm trying to creating a web app using GWT. In my application, I use MySQL as database. I tried to use a connection pool to control the number of connections to the database. However, Google web to...