Software Steeplechase

Hayden Steep’s development obstacle course. (Java, JEE, and beyond)

July 13, 2006

Why the Java compiler enforces catching some exceptions and not others

Filed under: Java

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.

4 Comments »

The URI to TrackBack this entry is: http://steep.blogsome.com/2006/07/13/why-the-java-compiler-enforces-catching-some-exceptions-and-not-others/trackback/

  1. Exceptions (with lower case e) that extend java.lang.Error are also unchecked.

    Comment by Tom Hawtin — July 15, 2006 @ 11:21 am

  2. 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

  3. Checked exceptions are the bomb. They rule.

    Comment by Anon — July 19, 2006 @ 5:08 am

  4. 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

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Jay of onefinejay.com