Why the Java compiler enforces catching some exceptions and not others
I use Eclipse as my Java IDE and when I invoke a method that throws an exception, Eclipse provides a nice compiler warning indicating that I must enclose the method in a try/catch block. The IDE (Java compiler) only warns me about some exception types and not others. For instance, if I were to invoke List.add(int, obj), it may cause an ArrayIndexOutOfBound exception, but the compiler doesn’t warn me about this. However, if I code File.createNewFile(), I am warned that IOException must be caught.
I understood the difference between checked exceptions VS unchecked exceptions, but didn’t know how the compiler differentiated. As it turns out, the short answer is very simple.
Unchecked Exceptions implement java.lang.RuntimeException
…while Checked Exceptions do not. (They directly extend java.lang.Exception or some subclass thereof).
Here are some great articles regarding exception handling.
Designing with exceptions, Java World documents how the existence of exceptions should be closely related to the role of the Class and how people are expected to use it.
The exceptions debate. To check, or not to check?, IBM developerWorks briefly covers checked vs unchecked exceptions and quickly gets into good criticisms of each pattern.
Exception debate among industry “names”, Old Joel on Software Forum post contains the views by people who are considered industry experts.
As far as the debate goes, there are very smart people who completely disagree whether exceptions should be declared as unchecked (runtime) or checked (compile time). I have yet to make up my mind.


