Java.net’s latest poll asks: should checked exceptions be removed from the Java language. Sadly, the poll is not going the way it should right now. Many people feel checked exceptions are key to reliable programming. They are wrong. Please: Read this post, and help improve the Java programming language by voting “Yes” to remove checked exceptions.
Checked exceptions is a concept that is unique to Java as a programming language. That is right: It was an experiment. I brave idea. It failed.
As I understand it, the popularity of checked exceptions in Java can only come from the misunderstanding that seems to be pretty prevalent, that the alternative to checked exceptions is no exceptions at all. That we would be back to error codes. Like the mkdir method and many others on the Java File class. This is of course just silly. Nobody wants to go back to not having exceptions, we just want to be rid of the checked kind. The ones that are unique to Java! The proponents of checked exceptions decry the decent into unreliable programs if we drop checked exceptions.
In all code I read, over and over again, checked exceptions reduce the reliability of a program. Almost no good error handling is done locally. With very few exceptions, error handling policies should be global, unless you have a very good reason to deal with a particular problem exactly locally.
But I am sick of all the comments singing the praise of checked exceptions on a theoretical basis. I don’t care about what’s good in theory! Look over your code, look over the code of your peers. Look at the
catch blocks. How many of them are just extra noise? How many of them ignore a potentially dangerous situation? How many of the simply wrap one exception in another exception? How many simply say “this can never happen”? How many contain bugs? How many are truly useful.
Now, look at the code in the standard library, books and articles. How many of these do you think actually improve the reliability and maintainability of the code? Look at the exceptions Sun decided to make checked. Look at CloneNotSupportedException, look at MalformedURLException, look at ParseException or UnsupportedEncodingException. Look at your code that deals with these exceptions. Are you happy that they were made checked exception? Does it seem like a good idea to force a programmer to deal with with the possibility that the object he’s foolishly trying to clone doesn’t implement an interface every time he tries to clone anything. Isn’t that what
ClassCastException is for?
Finally, take a look at the code base of the Spring framework. Is this unreliable code? This is the kind of code we will see if we get rid of checked exceptions. It’s the cleanest, most reliable code in the Java Open Source ecosystem.
I have yet to see code that was saved by a checked exception, but I have seen plenty of code that was hurt by it. 12 years is enough. End the suffering. Vote “yes” to get rid of checked exceptions!
Copyright © 2007 Johannes Brodwall. All Rights Reserved.