如何检测JVM出错异常退出?

How to detect that the JVM exited abnormally due to an error?

当使用错误的参数调用 JVM 时,我希望它以错误代码退出:

java -x && echo ok || echo Failed AS EXPECTED
Unrecognized option: -x
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Failed AS EXPECTED

现在,如果我使用另一个无法识别的参数,它会立即退出,但不会 return 错误代码:

# Now that's a CAPITAL X
java -X 2>/dev/null && echo ok || echo Failed AS EXPECTED
ok
  1. 为什么在这种情况下没有错误代码?
  2. 如果我没有收到错误,我如何检测到 JVM 没有机会 运行 代码?

没有报错因为-X(大写x)is a valid command line argument.

它使 Java 打印有关错误流的额外选项的帮助。