Difference in System. exit(0) , System.exit(-1), S

2019-01-02 22:00发布

I'd like to know the difference between the following in Java

System.exit(0);
System.exit(-1);
System.exit(1);

When do I have to use the above code appropriately?

11条回答
贪生不怕死
2楼-- · 2019-01-02 22:10

System.exit(0) by convention, a nonzero status code indicates abnormal termination.

System.exit(1) -It means termination unsuccessful due to some error

查看更多
甜甜的少女心
3楼-- · 2019-01-02 22:11

A good gotcha is any error code > 255 will be converted to error code % 256. One should be specifically careful about this if they are using a custom error code > 255 and expecting the exact error code in the application logic. http://www.tldp.org/LDP/abs/html/exitcodes.html

查看更多
贼婆χ
4楼-- · 2019-01-02 22:13

Zero => Everything Okay

Positive => Something I expected could potentially go wrong went wrong (bad command-line, can't find file, could not connect to server)

Negative => Something I didn't expect at all went wrong (system error - unanticipated exception - externally forced termination e.g. kill -9)

(values greater than 128 are actually negative, if you regard them as 8-bit signed binary, or twos complement)

There's a load of good standard exit-codes here

查看更多
成全新的幸福
5楼-- · 2019-01-02 22:15

System.exit(system call) terminates the currently running Java virtual machine by initiating its shutdown sequence. The argument serves as a status code.

By convention, a nonzero status code indicates abnormal termination.

  System.exit(0) or EXIT_SUCCESS;  ---> Success
  System.exit(1) or EXIT_FAILURE;  ---> Exception
  System.exit(-1) or EXIT_ERROR;   ---> Error

Read More at Java

On Unix and Linux systems, 0 for successful executions and 1 or higher for failed executions.

查看更多
Luminary・发光体
6楼-- · 2019-01-02 22:15

A non-zero exit status code, usually indicates abnormal termination. if n != 0, its up to the programmer to apply a meaning to the various n's.

From https://docs.oracle.com/javase/7/docs/api/java/lang/System.html.

查看更多
家丑人穷心不美
7楼-- · 2019-01-02 22:23

Here is the answer.

System.exit(0);// normal termination - Successful - zero
System.exit(-1);//Exit with some Error
System.exit(1);//one or any positive integer // exit with some Information message
查看更多
登录 后发表回答