System.out.println(Integer.parseInt(e.getMessage()));
System.out.println(e.getMessage());
System.exit(Integer.parseInt(e.getMessage()));
当我在unix system.exit(Integer.parseInt(e.getMessage()))
中运行代码时,给出了254
输出:
-2
-2
254
答案 0 :(得分:2)
您的操作系统的退出代码是无符号的8位整数,因此唯一有效的退出代码是0..255。
你得到254的原因是因为int
-2的低8位被视为无符号数。
$ for q in -2 -1 0 1 2 253 254 255 256 257 ; do
perl -e'exit $ARGV[0]' -- $q
printf '%3d => %3d\n' $q $?
done
-2 => 254
-1 => 255
0 => 0
1 => 1
2 => 2
253 => 253
254 => 254
255 => 255
256 => 0
257 => 1
答案 1 :(得分:2)
由于System.exit()需要在0-255之间取正值,-2
将变为256 - 2 = 254
。
答案 2 :(得分:1)
The exit status code on linux and Unix is 8 bits and unsigned (0-255)
此处从int
到byte
进行二进制级转换(254是-2整数的低级字节)。
请注意,这不会发生在java中(本机Shutdown.halt0
方法接收int),因为在不同的平台上可能会有所不同。