A Hopeful Idea: The End of Checked Exceptions?
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 instanceof
and 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!
Comments:
[Tor Bådshaug] - Jun 27, 2007
I couldn’t agree more, I think checked exception promotes bad code, like the buggy or “noisy” catch blocks mentioned. Also, to my experience, when repeatedly faced with the annoying “catch or throw”-complains from the compiler, most developers tend to find the quickest device to make those annoyances go away, such as an empty catch-block (ugh) or just use a throws declaration.
Speaking about throws declarations - you can choose whether to make them specific or general. Specific will tend to be implementation dependent, which is problematic for loosely coupled, interface-based architectures (as discussed here http://radio.weblogs.com/0122027/stories/2003/04/01/JavasCheckedExceptionsWereAMistake.html). It also raises versioning and compability issues.
General throws declarations (like throws Exception) just defeats the concept. That’s like implicit anyway if they were unchecked in the first place.
Most definitely a vote from me!
[Sverre H. Huseby] - Jun 26, 2007
As I tend to do what Johannes says, I just voted.
Tomas Ekeli - Jun 26, 2007
You make good points, in a shout-it-from-the-mountains kind of way. I don’t agree with you, but you make good points.
Johannes Brodwall - Jun 26, 2007
Hi, Tomas
Thanks for the comment and for the kind words. I take it that you have a different experience? I understand from your blog that you also program .NET Maybe you have a good story on what kind of problems you run into that would’ve been avoided with checked exceptions?
[Trond Arve Wasskog] - Jun 26, 2007
Hear hear! Checked exceptions is an abomination. Vote now!
Tomas Ekeli - Jun 26, 2007
Yes, indeed. You did give me an idea for a post :)
Bjørn Bjerkeli - Jun 30, 2007
Hi have been arguing for a long time, and I think most of us (that actually program i java, not just read articles bout it) agree that checked exceptions ar bad. Pretty disappointing to see however how the poll acually goes in favor of keeping them.
It seems that one of the major problems that the java-community is facing, is that devlopers tend to argue in favor of something that seems to be good in theory but that doesn’t necessarily work in practice. We need to keep this in mind, and we need write code and read code to actually understand this.
Thomas Kjeldahl Nilsson - Jun 26, 2007
Voted Checked exceptions = one of the more annoying java quirks after I got back into other languages on a daily basis. “Why do I have to deal with this crap?”