System.exit(num) or throw a RuntimeException from main?

前端 未结 7 1822
日久生厌
日久生厌 2020-12-18 19:57

I\'ve got a single threaded app that should set the DOS errorlevel to something non-zero if there is a problem. Is it better to throw a RuntimeException, or to use System.ex

7条回答
  •  臣服心动
    2020-12-18 20:18

    The APP itself should use System.exit. It's its interface with the calling environment (script). Any internal component of course should use Exception. When you put it together it can be the both of'em:

    Application.main(...) {
      parse(args);
      check(...);
      try {
        MyObject o = ...;
        o.doMyStuff();
      } catch (Exception e) {
        System.err.println("Oops, something went wrong!"); // by example, or use a logging framework! // anyway in a shell app System.in/out/err IS my interface with the outworld
        System.exit(ERROR_CODE);
      }
      System.out.println("Worked!");
    }
    

提交回复
热议问题