Best way to exit a program when I want an exception to be thrown?

If you let the exception propagate all the way up to the main() method, the program will end. There’s no need to call System.exit, just allow the exception to bubble up the stack naturally (by adding throws IOException) to the necessary methods.

Edit: As @Brian pointed out, you may want to catch the IOException in your main method, and call System.exit there instead, supplying a human-readable error message (stack traces can scare people). Also, as @MeBigFatGuy said, calling System.exit from inside your code stack is bad practice, and limits the reuseability of the code. If you must use System.exit, then keep it inside the body of the main method.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)