Coder Perfect

A non-void method with no return statement compiles.


I came into a case where a non-void method lacked a return statement but the code still compiles. I’m aware that the statements following the while loop are unreachable (dead code) and will never be run. But why doesn’t the compiler issue a warning when something is returned? Alternatively, why would a language allow us to have a non-void method that runs indefinitely and returns nothing?

public int doNotReturnAnything() {
    while(true) {
        //do something
    //no return statement

When I add a break statement to the while loop (even a conditional one), the compiler complains about the following errors: In Eclipse, a method does not return a value, while in Visual Studio, not all code paths return a value.

public int doNotReturnAnything() {
    while(true) {
        if(mustReturn) break;
        //do something
    //no return statement

Both Java and C# are guilty of this.

Asked by c.P.u1

Solution #1

For non-void procedures, the rule is that every code path that returns must return a value, and your program follows this criterion: zero out of zero code pathways that return do return a value. The rule isn’t that “any non-void procedure must have a return code path.”

This allows you to create stub-methods such as:

IEnumerator IEnumerable.GetEnumerator() 
    throw new NotImplementedException(); 

This is a method that isn’t void. In order to satisfy the interface, it must be a non-void method. However, making this implementation illegal appears to be a waste of time because it returns nothing.

It’s irrelevant that your method has an unreachable end point because you used a goto (remember, a while(true) is just a more pleasant way of writing goto) rather than a throw (which is another kind of goto).

Because the compiler has no reason to believe the code is incorrect. Someone wrote while(true), and it appears that whoever did so was aware of what they were doing.

Here are some of my articles on the subject:

ATBG: reachability (de facto and de jure)

Also, you might want to study the C# specification.

Answered by Eric Lippert

Solution #2

The Java compiler is capable of locating code that is inaccessible ( the code after while loop)

There’s no use in adding a return statement because it’s unreachable (after while ends)

Conditional if is the same way.

public int get() {
   if(someBoolean) {   
     return 10;
   else {
     return 5;
   // there is no need of say, return 11 here;

There is no need to supply a return explicitly after if-else because the boolean condition someBoolean can only evaluate to true or false, and Java does not complain about it.

Answered by sanbhat

Solution #3

The compiler understands that the while loop will never end, hence the method will never be completed, and thus a return statement is not required.

Answered by Daniel Hilgarth

Solution #4

Because your loop is executing on a constant, the compiler recognizes it as an endless loop, which means the procedure will never return.

The compiler will enforce the following rule if you utilize a variable:

This won’t compile:

// Define other methods and classes here
public int doNotReturnAnything() {
    var x = true;

    while(x == true) {
        //do something
    //no return statement - won't compile

Answered by Dave Bish

Solution #5

Unreachable statements are defined in the Java specification. Unreachable statements are not allowed in your code (it’s a compile time mistake). In Java, you can’t even have a return statement after the while(true); statement. You don’t require a return statement because the while(true); statement renders the following sentences inaccessible by definition.

While the Halting problem is insolvable irre the general situation, the definition of Unreachable Statement is more stringent. It’s determining very specific scenarios in which a program will not halt. The compiler cannot detect all endless loops or unreachable statements in theory, but it must detect certain circumstances described in the specification (for example, the while(true) scenario).

Answered by Konstantin Yovkov

Post is based on