2

just found an example on Oracle site, they use System.exit() if console is == null, but I can't see why it is better than simple return. Here's the link: http://docs.oracle.com/javase/tutorial/essential/regex/test_harness.html

skwisgaar
  • 880
  • 2
  • 13
  • 31
  • 1
    `System.exit(int)` sets the error level returned back to the process that ran the process, this provides the caller the opportunity to take appropriate action if the call fails for some reason... – MadProgrammer Sep 25 '14 at 02:36

3 Answers3

3

exit(1) means you are exiting from System and setting exit code = 1 means an erroneous termination of the program

whereas return; will terminate that application with exit code = 0, which to the caller means the program terminated successfully


Also See

Community
  • 1
  • 1
jmj
  • 237,923
  • 42
  • 401
  • 438
  • 1
    yes, I understand it in case of some relatively complicated application but using it by Oracle in such simple tutorial class seems to me a bit strange :) – skwisgaar Sep 25 '14 at 03:02
  • that is the only difference, probably the code was written from someone who came from long stay from c++ – jmj Sep 25 '14 at 03:22
1

System.exit(1) can be useful if you call your java program lets say from a bash script and you would like to react accordingly to success or failure of the java program.

karolovbrat
  • 116
  • 4
1

System.exit will shutdown all the executing threads, closing files, releasing resources, etc... It terminates the JVM.

However, Return is used by an executing thread on a method, but if system.exit is used, a thread will never return. So they are two different things that are used for different purposes.

See the Java 7 Oracle document link.

James Drinkard
  • 15,342
  • 16
  • 114
  • 137