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.



Exceptions (with lower case e) that extend java.lang.Error are also unchecked.
Comment by Tom Hawtin — July 15, 2006 @ 11:21 am
Thanks for the correction Tom. The only java.lang.errors I’ve experienced are OutOfMemory and StackOverflow. I didn’t realize throwable had 2 branches.
Comment by Hayden — July 15, 2006 @ 2:51 pm
Checked exceptions are the bomb. They rule.
Comment by Anon — July 19, 2006 @ 5:08 am
Those very smart people seem to agree that checked exceptions are not needed. I’m not sure why (maybe I’m not as smart), but it seems to be a healthy reminder that Eclipse will force a try/catch block or a throws clause.
If I were writing a database application and I knew there was a possible SQLException, I would write graceful handling code for this and a finally block for closing the connection to avoid connection pool leaks.
But when it comes to file system exceptions (like file system full, etc.) there’s not much I, as a Java programmer, want to do about this, so I would not “check” for those exceptions.
My comments may seem trite. However, I’ve attended many Java User Group presentations where this topic is discussed and debated.
Comment by Don Shade — July 19, 2006 @ 12:09 pm