An application crash at a glance at an exception is a very bad practice. Especially when some work is unsaved and the application uses some resources that need to be freed and cleaned up before the application completes execution. Some very popular software used for this ... and instead of "fixing" the problem, they introduced data recovery features when the application was restarted. However, the trick is not good software development.
At the very least, your application should not crash on the first error / error that occurred, but recover with a meaningful message. It's easy to just wrap everything in a RuntimeException (or even a Throwable ) and, especially, do nothing with it.
Java does not support flags of any type, because there are 1) a workaround and 2) the best ways to deal with this situation. For instance:
1. Handle the exception in the calling method
You can add the throws keyword to a method declaration, right down to your static public void main method, which, if it does not handle the exception, will ultimately crash the application using stacktrace.
class Foo { public void someMethod(....) throws IllegalArgumentException, IOException { ... } static public void main(String...args) throws Throwable { new Foo().someMethod(); } }
This method does not offer any means of recovery and is likely to make your user unhappy (with a big pointless stachtrace if they run the application from the console or just nothing if they run it from a shortcut or GUI). In addition, if you have some acquired resources, you cannot clear them when an exception occurs. At the very least, your main should catch (Throwable e) and output something before throwing the exception above. Sort of:
class Foo { public void someMethod(....) throws IllegalArgumentException, IOException { ... } static public void main(String...args) { try { new Foo().someMethod(); } catch (...) {
** EDIT **
Consider this scenario: the program initializes some resource to process some data, then during processing there is some exception (or error) at runtime, the application crashes, but the resources are not freed or not freed. However, in Java this can be done.
public E doSomething() throws RuntimeException {
and cleanly exit the method on error or on successful completion, possibly even registering a runtime error for the "offspring".
2. Write down the error and return some meaningful value
Registration is a very good practice. You can show your user some message telling them that the operation cannot be completed without failures, and gives you some traces of what and where the user is doing. A simplified registration system can be:
class Foo { static private final Logger LOG = Logger.getLogger(Foo.class.getName()); public boolean doSomethingImpl(...) { boolean result = true; try { ... } catch (SomeException e) { LOG.log(Level.SEVERE, "meaningful message why method could not do something!", e); result = false; } return result; } public void doSomething() { if (!doSomethingImpl(...)) {
By default, Logger outputs everything to err output, but you can add your own custom handlers:
Java already comes with some default registration handlers, one of which writes to a file .
and etc.