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

前端 未结 7 1806
日久生厌
日久生厌 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:09

    It depends how much information you want to report back to the script that starts your program. This can be very important if the script is designed to execute a chain of actions. https://shapeshed.com/unix-exit-codes/

    Example: I developed a Java program that calls an external API, downloads the response and saves it to a file. Possible outcomes:

    • 0 = OK
    • 5 = HTTP Temporarily unavailable
    • 6 = Unable to write file to disk

    Now my script knows what went wrong, and it could take different actions based on the outcome.

    • If response = 0, continue next step in the script
    • If response = 5, retry (with a delay)
    • If response = 6, stop the script

    Bottom line: like any good api, clearly define your input and output parameters and use System.exit.

    0 讨论(0)
  • 2020-12-18 20:15

    An exception thrown will print out the stack trace, and if you don't need that, you shoul use System.exit.

    Upon exit you can inform the user with a Sytem.out (I assume the app is running in a commanline environment only).

    You should consider just catching all errors an logging the errors in a seperate log, this ensures the stacktrace is not lost forever when you close the terminal. Take a look at log4j for this purpose, it's really easy to use.

    0 讨论(0)
  • 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!");
    }
    
    0 讨论(0)
  • 2020-12-18 20:20

    System.exit(num) is not a good option, as its shutdown JVM, plus even it didnt run the finally block if you have after catch block.

    Throwing RuntimeException also might not be the best of the option, can subclass as mentioned earlier which is app specific exception could be a better option in my opinion. -Manish

    0 讨论(0)
  • 2020-12-18 20:30

    Don't throw an exception unless you really have an exceptional condition. System.exit(int) is there for precisely this reason. Use it.

    EDIT: I think I may have misread your question. I thought you were asking, when you want to exit the JVM normally but signal that something did not quite go right, whether it is better to throw an exception or to use System.exit.

    However, if the problem that occurs is something which is already indicated by a Java exception, it's fine to just let that exception go unhandled. You don't have to catch the exception and call System.exit.

    If you have a choice of whether to throw an exception of your own or call System.exit, think about whether the error condition is something that might conceivably be handled by some Java code that calls your method. If the error occurs directly in the main method, then there will probably never be a caller to handle the exception so you should probably call System.exit. Otherwise, it's generally best to throw an exception - but not RuntimeException, you should probably use an exception type that appropriately represents the error you encountered. Write your own subclass of RuntimeException if necessary.

    0 讨论(0)
  • 2020-12-18 20:30

    System.exit() not recommended. It shutdowns JVM.

    0 讨论(0)
提交回复
热议问题