-2

I was looking for the ways to exit a method, i found two methods System.exit(); Return;

System.exit() - Exits the full program Return exits current method and returns an error that remaining code are unreachable.

class myclass
{
    public static void myfunc()
    {
        return;
        System.out.println("Function ");
    }
}

public class method_test
{
    public static void main(String args[])
    {
        myclass mc= new myclass();
        mc.myfunc();
        System.out.println("Main");
    }
}
hyde
  • 60,639
  • 21
  • 115
  • 176
RajaSekar
  • 398
  • 3
  • 8
  • 17
  • Exit kills the entire program. return just leaves the current method. – Chris Eberle Apr 27 '13 at 18:00
  • your code kills a good programmer because the return statement has been used at the top of the rest of the code I mean if you have used this before your code an at the very beginning then your rest of code will become unreachable – Tech Nerd Apr 27 '13 at 18:13
  • Personally, I come from an out school of thought. One entry, one exit. Each non-void method should have one `return` statement and it should be the last statement in the method. This makes the method easier to understand and read as you're not likely to miss a `return` statement hidden somewhere deep in some logic branch you weren't expecting, but that's just me – MadProgrammer Apr 27 '13 at 19:23

3 Answers3

3

The best and proper way to exit from method is adding return statement.

System.exit() will shutdown your programm.

if you use system.exit once a thread goes there, it won't come back.

system.exit is part of Design of the Shutdown Hooks API

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
3

There is no best way, it depends on situation.

Ideally, there is no need to exit at all, it will just return.

int a() {
    return 2;
}

If there is a real need to exit, use return, there are no penalties for doing so.

void insertElementIntoStructure(Element e, Structure s) {
    if (s.contains(e)) {
        return; // redundant work;
    }
    insert(s, e); // insert the element
}

this is best avoided as much as possible as this is impossible to test for failure in voids

Avoid system.exit in functions, it is a major side effect that should be left to be used only in main.

void not_a_nice_function() {
    if (errorDetected()) {
        System.exit(-1);
    }
    print("hello, world!");
}

this pseudocode is evil because if you try to reuse this code, it will be hard to find what made it exit prematurely.

Dmytro
  • 5,068
  • 4
  • 39
  • 50
  • But Remaining codes after the return statement results an error method_test.java:6: error: unreachable statement System.out.println("Function "); – RajaSekar Apr 27 '13 at 18:08
  • That only happens if the premature return is unconditional. If it's conditional, java will recognize that it can be reached. – Dmytro Apr 27 '13 at 18:10
  • Basically, you need to make an if statement, otherwise java is smart enough that the code will never be reached (due to unconditional jump). – Dmytro Apr 27 '13 at 18:12
2

first of all your code will kill good programmers imagine this code Which is the Best way to exit a method this code example that how a return comes before a System.out.print(); as it becomes unreachable after the return statement lols the command

         System.exit(int status); (status=0 for Normal Exit && status=-1 for abnormal exit

is only used if you want to exactly quit your whole app whereas the command

         return;

is used to get out/return from a method these two are different in their operations

Community
  • 1
  • 1
Tech Nerd
  • 822
  • 1
  • 13
  • 39