System.exit(1) and return
They do different things. return
simply returns from the function to its caller and the program continues running. System.exit()
terminates the program; control does not return to the caller.
You should use System.exit()
when your program encounters an unrecoverable error and there is no point in the program continuing to run. Use return
when your program can gracefully recover, or when the program should perform cleanup/closeout actions before exiting.
See also this more extended discussion of System.exit()
.
Difference between killing a process and System.exit(0);
System.exit(1);
Usually a non-zero error status indicates that the program ended abnormally and
int id= android.os.Process.myPid();
Process
is Tools for managing OS processes.
android.os.Process.killProcess(id);
Kill the process with the given PID. Note that, though this API allows us to request to kill any process based on its PID, the kernel will still impose standard restrictions on which PIDs you are actually able to kill. Typically this means only the process running the caller's packages/application and any additional processes created by that app; packages sharing a common UID will also be able to kill each other's processes.
Use of System.exit(0)
From the JAVA Documentation:
The argument serves as a status code;
by convention, a nonzero status code
indicates abnormal termination.
And Wikipedia adds additional information.
Why should we inform the JVM with exit status like exit(0), exit(1)?
The exit code is not for the JVM. It is for the user / system running the program.
An exit code of 0
indicates that the program executed fine. An exit code of anything else represents an error. As the developer you can decide what the different codes mean, for example you could decide that a code of 4
meant a database error caused the exit.
You can find a bit more information here.
Execute a form after System.exit (0);
You should manually keep track of all "forms" in a list somewhere, then just iterate over that list and close them.
If for whatever reason this is not possible you could use JFrame.getWindows() which accesses all Windows in the application
for (Window each : JFrame.getWindows()) {
each.setVisible(false);
}
Note: This may not properly shutdown the forms, e.g. remove listeners/stop threads the forms may be using...
Related Topics
Populating Spring @Value During Unit Test
Differencebetween 'Java', 'Javaw', and 'Javaws'
List.Clear() VS List = New Arraylist<Integer>();
How to Reference Another Property in Java.Util.Properties
Missing Return Statement for If/Else Statement
Get Only Part of an Array in Java
Printing Java Collections Nicely (Tostring Doesn't Return Pretty Output)
How to Keep a User Logged into My Site for Months
Get All of the Classes in the Classpath
Java Streams: Replacing Groupingby and Reducing by Tomap
Reflection Generic Get Field Value
Synchronizing on an Integer Value
How to Fix Ambiguous Type on Method Reference (Tostring of an Integer)