Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.exit(nonzero)? I don't need the stack trace, and I don't expect this app to be extended/reused. What are the differences between these two options?

like image 659
VarV Avatar asked Aug 18 '10 23:08

VarV


People also ask

When should you throw a RuntimeException?

One case where it is common practice to throw a RuntimeException is when the user calls a method incorrectly. For example, a method can check if one of its arguments is incorrectly null . If an argument is null , the method might throw a NullPointerException , which is an unchecked exception.

Is it good practice to throw RuntimeException?

Additionally, catching RuntimeException is considered as a bad practice. And, thus, throwing Generic Exceptions/Throwable would lead the developer to catch the exception at a later stage which would eventually lead to further code smells.

What does the system exit () do?

System. exit() method. This method terminates the currently running Java Virtual Machine(JVM). It takes an argument “status code” where a non zero status code indicates abnormal termination.

Is it good to use system exit?

exit() it will do so with a non-zero status (as long as the main thread ends and there are no running daemon threads). That's all about Why you should not use System. exit() inside Java application. It can be dangerous and potentially shut down the whole server, which is hosting other critical Java application.


2 Answers

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.

like image 54
David Z Avatar answered Sep 20 '22 05:09

David Z


Generally in this situation I would handle all exceptions in my main method, possibly by calling System.exit. This gives you flexibility about where/whether/how to handle exceptional conditions, while still meeting your need to terminate with an error code. In particular, it gives you control over the return code and any other output you might generate for the user (error message, stack trace, etc). If you throw an exception in main (or let an exception escape), you lose that control.

To summarize, call System.exit only in your top-level exception handler:

static public void main() {    try {       runMyApp();    } catch (Exception e) {       System.exit(1);    } } 
like image 38
Aaron Novstrup Avatar answered Sep 22 '22 05:09

Aaron Novstrup