difference between System.exit(0) and System.exit(-1) [duplicate]

六眼飞鱼酱① 提交于 2019-12-09 06:52:38

问题


Can any one share to me difference between System.exit(0) and System.exit(-1) it is helpful if you explain with example.


回答1:


It's just the difference in terms of the exit code of the process. So if anything was going to take action based on the exit code (where 0 is typically success, and non-zero usually indicates an error) you can control what they see.

As an example, take this tiny Java program, which uses the number of command line arguments as the exit code:

public class Test {    
    public static void main(String[] args) throws Exception {
        System.exit(args.length);
    }
}

Now running it from a bash shell, where && means "execute the second command if the first one is successful" we can have:

~ $ java Test && echo Success!
Success!

~ $ java Test boom && echo Success!



回答2:


System.exit(0) means it is a normal exit from a program.But System.exit(-1) means the exit may be due to some error. Any number other that zero means abnormal exit.




回答3:


The parameter of System.exit(int) is the return value of your program, which can be evaluated in batch jobs (usually for console programs). By convention, every value other than 0 inidaces that something went wrong.




回答4:


If you run your Java program in Linux/Unix you can examine the result using echo $? command. This is why it is important to call System.exit(0) (this is done for you if you don't) if everything is fine and System.exit(non-zero) otherwise.



来源:https://stackoverflow.com/questions/6937660/difference-between-system-exit0-and-system-exit-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!