Reputation: 977
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
Upvotes: 2
Views: 1395
Reputation: 240900
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
Upvotes: 3
Reputation: 15703
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.
Upvotes: 1
Reputation: 116
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.
Upvotes: 1