Cleaning Up After Yourself – Java Style

When it comes to developing applications, we as software developers always (almost always at least!) stive to make our code as robust and clean as possible.  By robust I mean error conditions are handled gracefully and clean meaning that whatever resources we have opened up are closed appropriately. But let’s face it, there will be times when mistakes are made or the unexpected happens. Fortunately, Java provides some great tools that allow for handling just such situations – shutdown hooks and uncaught exception handlers.

Shutdown Hooks

Shutdown hooks are Threads that are initialized, but not started.  When the virtual machine begins to shutdown it will run all registered shutdown hooks, but not in any specified order. Registering a shutdown hook is trivial:
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { doSomeCleanup(); } }));

Now when a program exits, the code specified in your shutdown hook will run, giving you one last chance to ensure things are left in a consistent state. There are some caveats with shutdown hooks:

  1. Don’t assume other services will be available, code defensively
  2. Make every attempt to make sure the code executes quickly
  3. Make sure the code is written to avoid deadlocks

Ideally, shutdown hooks are to ensure that resources are cleaned up properly and not a replacement for good coding practices.

Uncaught Exception Handlers

The Thread.UncaughtExceptionHandler represents a chance to do what the name of the interface implies, handle any uncaught exceptions. As with the shutdown hook, the UncaughtExceptionHandler should not be used as an excuse to get away from good coding practices. Rather, the uncaught exception handler is the last line of defense against the truly unexpected exceptions. Setting an UncaughtExceptionHander is equally as easy:

Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){ @Override public void uncaughtException(Thread thread, Throwable throwable) { handleTheUnexpectedError(); } });

Now with just four lines of code, you have given your program the ability to handle unexpected errors gracefully.
I hope the reader found these tips useful and have a great Christmas!

Meta: this post is part of the Java Advent Calendar and is licensed under the Creative Commons 3.0 Attribution license. If you like it, please spread the word by sharing, tweeting, FB, G+ and so on! Want to write for the blog? We are looking for contributors to fill all 24 slot and would love to have your contribution! Contact Attila Balazs to contribute!