6

Why the result of the following code is 3, why the finally get to terminate and get out of the method even tho the compiler checks try first and why the return in try doesn't terminate the method?

public int returnVal(){ 
    try{
        return 2;
    }
    finally{
        return 3;
    }
}
malaguna
  • 4,183
  • 1
  • 17
  • 33
Ahmad Sanie
  • 3,678
  • 2
  • 21
  • 56
  • 1
    The `finally` block is always called at the end, that's why it's called "finally" (think of "try this, catch any exception and finally do that"). – Thomas Jan 11 '16 at 15:02
  • 2
    The `finally` block is *always* executed no matter what.. – Gaël J Jan 11 '16 at 15:02
  • yeah i understand that but why the compiler is ignoring the return statement in the try even tho it gets into it at run time why the return in try doesn't terminate the method – Ahmad Sanie Jan 11 '16 at 15:03
  • The `finally` block is always executed _unless_ you do a `System.exit()` or the JVM crashes. – Jonas Czech Jan 11 '16 at 17:07

5 Answers5

5

See JLS 14.17

It can be seen, then, that a return statement always completes abruptly.

The preceding descriptions say "attempts to transfer control" rather than just "transfers control" because if there are any try statements (§14.20) within the method or constructor whose try blocks or catch clauses contain the return statement, then any finally clauses of those try statements will be executed, in order, innermost to outermost, before control is transferred to the invoker of the method or constructor. Abrupt completion of a finally clause can disrupt the transfer of control initiated by a return statement.

Espacially check the phrases attempts to transfer control and the last sentence. The try return attempts to transfer the controll, aftwerwards the finally disrupt the transfer of control initiated by a return statement.

In other words, the try attempts to transfer the controll, but since the execution of the finally block is still open for execution and contains a return statement the attempted transfer of controll in the finally block has a higher preceding. That´s why you see the value 3, which is returned inside the finally block.

Community
  • 1
  • 1
SomeJavaGuy
  • 7,307
  • 2
  • 21
  • 33
4

finally is executed before returning value from try (or catch).

If in the finally a return statement is present it overwrites the return of the try (or catch) block.

So also in this case the return of finally wins:

try {
    // Exception thrown
    return 2;
} catch (Exception e) {
    return 1;
} finally {
    return -1;   // Always returns -1 also if a return statement is present in the try and in the catch clause
}
Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
  • +1 as it was a lot easier to understand than the top answer (at the price of less information, of course). – domsson Jan 11 '16 at 15:40
1

Why the return in try doesn't terminate the method?

Is it bad practice to return from within a try catch finally block?

You have to be aware that when you put a return inside a try-catch statement, the return itself is held and managed by the exeption handling block.

The exception block only STORES your return and it really returns it ONLY after all the verifications are done, which doesn't mean that the finally block will really have an impact on your return:

public int returnVal() { 
    int i = 0;
    try {
        return i;
    }
    finally {
        i = 99;
    }
}

In this case, the return will still be 0.

But in the case you mentioned in your question, you affect the return a second time, and as the exception block dosen't want to let your return go until it's done with ALL its verification, you're just always storing a return value 2 times in your exception block. The result will always be 3.

Just keep in mind that if you put a return inside a try-catch-finally, that return will not return until the finaly block has run; the return itself is just being stored until everything has been verified.

DaveyDaveDave
  • 9,821
  • 11
  • 64
  • 77
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
0

finally explication when you write a finnaly block these always execute it.

Mathew Rock
  • 989
  • 2
  • 14
  • 32
0

The finally block is always executed before control is returned to the calling function. So in this case before returning 2, it will call finally and will return 3.