user1016403
user1016403

Reputation: 12621

Returning value from catch/finally in java?

I have below code. if query is executed without exception then true should return, if any exception is thrown then false should be returned. and finally statement and connection should be closed. my question is where should i write return statement? in catch or finally? in below code i am returning true in try if query is executed, and false in catch if any exception is thrown. my question is if any exception is thrown, does returning false and closing connection and statement happens?

try {
            statement = connection.createStatement();
            statement.executeQuery("select * from dual");
            return true;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
                   return false;
        } finally{
                try {

                        statement.close();

                        connection.close();

                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();

                }

        }

Upvotes: 3

Views: 7781

Answers (5)

Nim
Nim

Reputation: 33645

It won't work as you've written it. To execute the return with the finally block properly, you need to keep state, i.e.

boolean result = false;

try
{
 // all good
 result = true;
} 
catch(...)
{
 // crap
 result = false;
}
finally
{
 // close
}
return result;

This assumes that you want what's in the finally block to be executed and the correct result to be returned.

Upvotes: 4

Mayank Pandya
Mayank Pandya

Reputation: 1623

You can try

boolean returnVal = false
try {
            statement = connection.createStatement();
            statement.executeQuery("select * from dual");
            returnVal = true;
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    returnVal = false;
    } finally{
            try {

                    statement.close();

                    connection.close();


            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
         returnVal = false;

            }
    return returnVal;

    }

Upvotes: 1

memo
memo

Reputation: 441

Better yet try using JDK 7 and use the new try with resources.

Upvotes: 1

NPE
NPE

Reputation: 500673

where should i write return statement?

Put it right at the very end, after the final }:

    try {
        ...
        return true;
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        ...
    }
    return false; // <===== HERE

This way the function would return true if and only if the code within the try blocks executes without throwing an exception. In all other cases, the function would return false.

Upvotes: 5

Marko Topolnik
Marko Topolnik

Reputation: 200206

In neither, write it outside the try-catch block, below it. NEVER USE RETURN IN THE FINALLY BLOCK as that return statement would be executed always, trampling over your regular return statement in the try-block!

Upvotes: 8

Related Questions