Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to have return statement in try block when returned value from finally block is concerned

Tags:

I was wondering, is it good practice to return from try block?

package debug;

/**
 *
 * @author Owner
 */
public class Main {

  public static void main(String[] args) {
    System.out.println(fun());
  }

  static boolean cleanup() {
    // Fail to cleanup.
    return false;
  }

  static boolean fun() {
    boolean everything_is_fine = true;
    try {
      System.out.println("open file stream");
      return everything_is_fine;
    } finally {
      everything_is_fine = cleanup();
    }
  }
}

I first thought false will be printed. However, here is the output :

open file stream
true

As you can see, if I am having return statement within try block, I will miss the fail status during finally cleanup.

Shall I have the code as :

  static boolean fun() {
    boolean everything_is_fine = true;
    try {
      System.out.println("open file stream");      
    } finally {
      everything_is_fine = cleanup();
    }
    return everything_is_fine;
  }

As long as the returned value from finally block is concerned, shall I avoid return from try?

like image 967
Cheok Yan Cheng Avatar asked Sep 04 '10 16:09

Cheok Yan Cheng


People also ask

Is it good practice to return from TRY block?

In general both block should declare an exit. If in try you have return you should have also it in catch, in your case the second return was replaced with throw.

Can we have return statement after finally block?

Yes, we can write a return statement of the method in catch and finally block.

What if there is a return statement in try block followed by finally block?

Yes, the finally block will be executed even after a return statement in a method. The finally block will always execute even an exception occurred or not in Java.

What will happen when try and finally block both return value?

When try and finally block both return value, method will ultimately return value returned by finally block irrespective of value returned by try block.


2 Answers

Your suggested code (at the end of the question) is fine. You can return from the finally block, but you should not - for example eclipse shows a warning "finally block does not complete normally".

In fact, the try/finally aren't directly related to the return. It seems so here, because it is the only construct in the method, but you can have other code after that (for example - event notifications), and then return.

As for your question - you can't change the value of the returned variable in the finally block if it is already returned. So don't return from try.

like image 92
Bozho Avatar answered Sep 26 '22 09:09

Bozho


The return statement dictates what value is being returned, which at the time the return statement is executed is true. finally does change the value of the variable everything_is_fine, but that doesn't change what the already executed return statement returned.

You could add another return in finally which will override the return inside try:

static boolean fun() {
    boolean everything_is_fine = true;

    try {
      System.out.println("open file stream");
      return everything_is_fine;
    } finally {
      everything_is_fine = cleanup();
      return everything_is_fine;
    }
  }

However, the use of finally to modify the control flow is not considered good practice. It is certainly possible though. A better way of doing this in your case would be:

static boolean fun() {
    boolean everything_is_fine = true;

    try {
      System.out.println("open file stream");
    } finally {
      everything_is_fine = cleanup();
    }

    return everything_is_fine;
  }

Btw, the variable name should be changed to everythingIsFine per the prevailing Java naming conventions ;-)

like image 31
samitgaur Avatar answered Sep 25 '22 09:09

samitgaur