Put method return before a "Finally" block

Asked

Viewed 360 times

4

Considering:

try {
    System.out.println("Parte 1");
    return false;
} finally {
    System.out.println("Parte 2");
}

What will be the output and what happens behind the scenes so that the output come out that way?

  • 3

    Are you sure this is the question you want to ask? Wouldn’t it be something like asking why it happens, for example?

  • @Piovezan, I agree, but I think it’s too late to change the question at this level.

  • I disagree. If you change, then yes the answer will make sense, because they already answer this even without you have put in your question. The way she is, she’s very poor.

2 answers

8


The intention of finally is to ensure that this block is executed under any circumstances (unless every platform has some catastrophic behavior, of course, or by a System.exit()). So no matter what happens in the method, it will be executed. No matter if it will come out by return or throw.

Then the way out will be:

Parte 1
Parte 2

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Documentation.

Rewriting

This block is rewritten by the compiler (it looks something like this, in C# it looks like this). It would look something like this:

try {
    System.out.println("Parte 1");
} finally {
    System.out.println("Parte 2");
}
return false;

Already

try {
    System.out.println("Parte 1");
    return false;
} finally {
    System.out.println("Parte 2");
    return true;
}

Will be transformed into:

try {
    System.out.println("Parte 1");
} finally {
    System.out.println("Parte 2");
}
return true;

I put in the Github for future reference.

Note that the execution of a finally takes precedence when instructions are conflicting. If there is a return within a catch, will precede the return of try but will lose to the finally.

Completion

Anyway there is the guarantee of execution. The actual implementation is not important since it could change. The important thing is to know that there is the guarantee of the execution of the block finally before exiting the method, therefore before the return of try. Evidently if there is any expression on return it will be saved in temporary variable to avoid execution at wrong time in reordering instructions.

5

The output will be:

Parte 1
Parte 2

And then false will be returned.

The block finally executes whenever the block try ends, even if he has released an exception, returned, aborted a loop or something like that.

Internally, the JVM saves the returned value into a local variable that it invents for this, executes the finally and then returns the value of the local variable. It does the same for exceptions.

  • 2

    Anything like that but a System.exit().

  • 1

    @Piovezan Sim, System.exit() is the big red button. But in this case, the block try doesn’t even finish.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.