Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple return statements without compiler error

People also ask

Is it bad practice to use multiple return statements?

It is sometimes said that a method should have only one return statement (i.e. one exit point) and that to code using more than one return per method is bad practice. It is claimed to be a risk to readability or a source of error. Sometimes this is even given the title of the “single exit point law”.

Can you have multiple return statements in a function?

A function can have more than one return statement, but only ever run one based on a condition.

Can you have multiple return statements in C++?

You can have multiple return statements and it should not matter, if you are returning the same variable. However if you have multiple variables in your function and you return then differently depending on the code flow the compiler cannot perform such optimisation.

How many times return statement can be written in a function body?

More than one return statement may appear in a function, but only one will ever be executed by any given function call. ( All returns other than the last need to be controlled by logic such as "if" blocks. )


It does not give a compilation error because it is allowed by the Java Language Specification. However, it gives a warning message because including a return statement in the finally block is usually a bad idea.

What happens in your example is the following. The return statement in the try block is executed. However, the finally block must always be executed so it is executed after the catch block finishes. The return statement occurring there overwrites the result of the previous return statement, and so the method returns the second result.

Similarly a finally block usually should not throw an exception. That's why the warning says that the finally block should complete normally, that is, without return or throwing an exception.


This is described in the Java Language Specification:

§14.17

Abrupt completion of a finally clause can disrupt the transfer of control initiated by a return statement.

§14.20.2

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 abruptly for reason S (and reason R is discarded).

There are no compile time error since only 1 and exactly 1 of return statement will actually return the control back to calling code.

As explained @Hoopje, return within try or catch will execute first, their respective return statement will also execute. But just before returning the control back to calling code, it will execute the finally block. Now, this block also returns something, so this return overrides the previous one.


It's essentially the same as this:

public boolean someMethod(){
        if(1 == 1){
            return true;
        }
        return false;
}

It will not give a compilation error, although it will give a warning. The compiler will only give an error when there's a chance of no return statement being executed.