1

I was wondering if it's possible to exit a function early, bypassing the remainder of the code in the body. For example:

  public class SOMECLASS {

      public SOMECLASS( int i ) {
          gotoSwitchCase(i);
          additionalTask();    // method body is irrelevant
      }

      private void gotoSwitchCase(int i) {
          switch (i) {
          case 0:
              /* exit ctor prematurely  */
              break;
          default:
              /* set some instance variables, return to ctor */
          }
      }
  }

I know that I can insert if(i==0){return;} at the very beginning of additionalTask() to skip that part. However, I'm more interested in whether or not there is a way to exit the constructor (or any function) gracefully without visiting the rest of it's body. Similar to an exception, but not as fatal.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
touch my body
  • 1,634
  • 22
  • 36
  • 3
    But exceptions aren't fatal if you catch them... – Aify Feb 24 '15 at 23:08
  • 3
    how is `gotoSwitchCase` supposed to know it's being called from a constructor rather than anywhere else? In the general sense, `return` is, by definition, exactly how you exit a method gracefully. There's no requirement that it's the last line in a method body at all, though some coding styles prefer a [single point of exit](http://stackoverflow.com/questions/4838828/why-should-a-function-have-only-one-exit-point) – aruisdante Feb 24 '15 at 23:10
  • @aruisdante Fair point. I was wondering if there was a way to exit it via the `gotoSwitchCase()`, but that seems unlikely. A `return` statement would only exit the `void gotoSwitchCase()` if called from there. Perhaps it's impossible without modifying the constructor. Thanks for the input. – touch my body Feb 24 '15 at 23:24
  • 2
    From a design standpoint, imagine how horrible it would be to have to figure out if a called method could arbitrarily cause the calling method to also exit? Of course methods which throw exceptions can technically do this, but then either the calling method must catch the exception or must itself throw and this is enforced by the compiler. I cannot think of a single design that would need this pattern for a 'normal' return that couldn't be redesigned to not need it. In fact many people make the same argument for Exceptions, but in that case the added clarity is generally considered useful ;) – aruisdante Feb 24 '15 at 23:34

3 Answers3

8

If you wish to exit a function early (including the constructor), you can simply use the return; command. This works in constructors as well.

Edit for clarification: This is the correct way to solve this problem. You can't skip skip the rest of the constructor from the body of gotoSwitchCase because gotoSwitchCase could be run from the constructor or any other method from which it is visible.

John Bergman
  • 128
  • 8
  • 1
    Indeed, though the OP seems to want a `return` from `gotoSwitchCase` to somehow bubble out to the constructor automatically like an uncaught exception would, except without the exception part (which is impossible). – aruisdante Feb 24 '15 at 23:16
  • @Aify that is possible (like in your solution), however this does not answer the question OP asked which is _is it possible to exit a constructor without visiting the rest of its body_. Adding the conditional statement will continue running the body. – John Bergman Feb 24 '15 at 23:21
3

Here's an easy workaround: Return codes. Return 0 to skip the next line.

public class SOMECLASS {

    public SOMECLASS(int i) {
        if (gotoSwitchCase(i) != 0) {
            additionalTask();
        }
    }

    private int gotoSwitchCase(int i) {
        switch (i) {
            case 0:
                return 0;
            default:
                /* set some instance variables, return to ctor */
                return 1;
        }
    }
}

Also, exceptions aren't fatal if you catch them. You can do Try/Catch in the constructor, and throw from gotoSwitchCase. If you caught the thrown exception in the Constructor, nothing under it will run so you get the same effect.

Of course... you could always just use return wherever you need to (including constructors)

Aify
  • 3,543
  • 3
  • 24
  • 43
1

No, you cannot return the execution of the parent function. But your gotoSwitchCase(...) function can return -1 if you want to exit prematurely. Then you just have to do a if(gotoSwitchCase(...) == -1) return;

Jordan Martin
  • 176
  • 2
  • 5