Home Java Java-tips Flow Exceptions Exceptions - More

Ask Questions?

View Latest Questions


 
 

Exceptions - More
Posted on: July 26, 2006 at 12:00 AM
There are many exceptions, but they can be put into two groups: checked exceptions and unchecked exceptions

Java Notes

Exceptions - More

Exceptions | Exception Usage | Exceptions - More

Kinds of Exceptions

There are many exceptions, but they can be put into two groups: checked exceptions and unchecked exceptions. There is some controversy about which type you should use. A discussion of some of the issues can be found at Java theory and practice: The exceptions debate.

  • Unchecked Exceptions -- These exceptions are usually something that should have been prevented by more careful programming. For example, you should never get NullPointerException or ArrayIndexOutOfBoundsException. If you do, there is something wrong with your program, and you need to fix it. You usually don't catch unchecked exceptions. Instead, fix your program so it can't produce one of these. However, NumberFormatException is the one exception of this type that is usually caught.
  • Checked Exceptions -- These are usually errors in the input data. The programmer has no control over the input the user gives you, eg, file names, .... If the user gives you a bad value, it may cause an exception when you use it. You need to check for bad input using a try statement.

Use exceptions for exceptional conditions, NOT normal control flow

Probably most of your student programming has been "Happy Trails" style, where you didn't have to worry much about handling errors. But error handling is really a big deal in most real programs, and exceptions play a central role in dealing with errors.

All experienced programmers agree that using exceptions for normal processing flow is wrong. Exceptions should only be used only for errors or unusual conditions, and the equivalent if tests should be used for normal processing