Use exceptions with care

The JavaWorld experts answer your most pressing Java questions -- every week

Q: Please explain the following sentence: 'Use exceptions where you really need them -- not only do they have high basic cost, but their presence can hurt compiler analysis.'

A: Only when the situation is truly exceptional should you use this mechanism, though you may be tempted to throw exceptions simply to take advantage of their control-flow aspects. A good use of this mechanism would be to throw an exception if a file could not be found; a poor use would be to throw an exception with the name of a file that had been found. In the former case, the cost of the mechanism would only be incurred when the app was unable to proceed normally; in the latter case the cost would be incurred all the time without any added benefit.

Random Walk Computing is the largest Java/CORBA consulting boutique in New York, focusing on solutions for the financial enterprise. Known for their leading-edge Java expertise, Random Walk consultants publish and speak about Java in some of the most respected forums in the world.

Learn more about this topic