Basic Java Optimisation Hints

7. Never ignore Exceptions

Okay, okay, we've all done it. As a quick and dirty way to make a piece of code compile you ignore the exceptions and end up with something like:

    try {
      dodgeyMethodCall();
    }
    catch (Exception e) {
      ;
    }

It keeps the compiler happy while you get on with worrying about the rest of your algorithm.

That dodgey method full of bug could be throwing exceptions all the time and you'd never know.

Common fix is to dump the exception out to the System.err stream

But if you're running a Wedge application in full screen mode you probably won't be looking at the console output very much

It keeps the compiler happy while you get on with worrying about the rest of your algorithm. Trouble is, that dodgey method could be throwing exceptions all the time and you'd never know. That's bad for all sorts of obvious reasons, but also because exception handling is expensive and so your performance will take a hammering. The cost of the method invocation blows out, an Exception object is created (and then ignored), and who knows what knock on effects will occur if dodgeyMethodCall does part but not all of what it should.

The common fix is to dump the exception out to the System.err stream. That's better, but you can fall into the same problem if you don't actually bother checking the text output of your code as it's running. This may seem unlikely, but if you're running a Wedge application in full screen mode you probably won't be looking at the console output very much. That makes it easy to miss important exception traces.

So the lesson is never ignore exceptions: either in your code, or when they're reported to you in your console