0

I know that finally block executes even if code throws exception or it completes successfully now my doubt is this code

returns 40

which is obvious, but now if I

comment return 40;

it returns 10

can anybody help me understand how JVM returns 10 why it does not complain saying that function should return a value.

public class ExceptionTest {
        public static void main(String[] args) {
        int i=  returnSomething();
        System.out.println(i);
        }
    
        private static int returnSomething() {
            
            try{
                System.out.println("try");
                return 10;
            }
            catch(Exception e)
            {
                return 20;
            }
            finally
            {
                System.out.println("finally ");
                return 40;
            }
        }
    
    }
Community
  • 1
  • 1
ankit
  • 4,919
  • 7
  • 38
  • 63
  • You can have a look at this [question](http://programmers.stackexchange.com/q/188858). It might clear all your doubts! – Rahul Oct 23 '13 at 04:54
  • http://stackoverflow.com/questions/15616984/return-statement-in-java-exception-handling – VAr Oct 23 '13 at 05:01

4 Answers4

2

Just before executing the return in the try block, the control gets transferred to the finally block. Upon execution of the finally block, the function returns unless you have a return in the finally block itself.

As such, the function returns 40 when you have return in the finally block, and returns the value in the try block when you don't return in the finally block.


It is not recommended to return in the finally block.

devnull
  • 118,548
  • 33
  • 236
  • 227
  • A clean, short description. Nearly perfect, (and it got my vote), but you might want to alter the wording "Upon reaching the ..." to "Just before executing the return in the try block...". Upon reaching is just a touch vague in indicating whether it's just before executing, or during execution of. – Edwin Buck Oct 23 '13 at 05:19
  • @EdwinBuck Thanks. Updated to get rid of the ambiguity. – devnull Oct 23 '13 at 05:21
1

According to the Java Language Specification, Section §14.20.2

A try statement with a finally block is executed by first executing the try block. Then there is a choice:

  • If execution of the try block completes normally, then the finally block is executed, and then there is a choice:
    • If the finally block completes normally, then the try statement completes normally.
    • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S.

...

  • If execution of the try block completes abruptly for any other reason R, then the finally block is executed, and then there is a choice:
    • If the finally block completes normally, then the try statement completes abruptly for reason R.
    • If the finally block completes abruptly for reason S, then the try statement completes bruptly for reason S (and reason R is discarded).

When there is no return in the try block, then the finally block is executed and the return in the finally block will be used.

When there is a return in the try block, then it can be conscidered that the try block finished abruptly, however, according to the JLS, the finally block will still be executed before the code returns.

You can also refer to Section §14.17 of the JLS to help understand the return statement.

Of interest, the note at the bottom of the section:

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.

Francis
  • 1,090
  • 7
  • 12
0

finally block execute after try and catch block. So, if you comment return 40 than it will return try block value if no exception occur. Thats why it return 10.

If exception occur than return catch block value of 20.

Masudul
  • 21,823
  • 5
  • 43
  • 58
0

This is a good read on the finally block.

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

Jonny Henly
  • 4,023
  • 4
  • 26
  • 43