October 10, 2009

Difference between System.exit(0) and System.exit(1)

System.exit(n) terminates the process and sets the process exit code to 'n' . The significance of the value of 'n' depends on what the programmer wants the significance to be BUT most processes return 0 if there is no error and any other value if there is an error.(OR)
When a program terminates it generate a return code, and this includes the java program. This is passed to the command shell of the operating system, which can do with it as it choses (more often than not, nothing).
If you are running your java program from a batch file/shell script then your script can test the return code on the next line. Typically a program indicate a successful termination with 0, and various kinds of abnormal termination with different number.

For example you might do System.exit(1) after telling the user the command line arguments are invalid.
()OR)
Classes can contain static member subroutines, as well as static member variables. For example, the System class contains a subroutine named exit. In a program, of course, this subroutine must be referred to as System.exit. Calling this subroutine will terminate the program. You could use it if you had some reason to terminate the program before the end of the main routine. (For historical reasons, this subroutine takes an integer as a parameter, so the subroutine call statement might look like "System.exit(0);" or "System.exit(1);". The parameter tells the computer why the program is being terminated. A parameter value of 0 indicates that the program is ending normally. Any other value indicates that the program is being terminated because an error has

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts