The checked vs unchecked exceptions never ending wars
2 Comments Published June 20th, 2007 in Java Tags: .Many times people complains about checked exceptions in Java, and how wonderful the world is in C# where exceptions are all unchecked.
I’m much in favor of checked exceptions: I want to know what a method can return having a knowledge as complete as possible on method behaviors.
Unchecked exceptions sounds like that any method returns java.lang.Objects or not declaring any return type at all. Like in JavaScript: you should already know that a function may eventually return something or some exception: it’s scary! I hope Java will continue having checked exceptions forever: all we need against bugs is control over the code and such kind of automatism make you loose that. I imagine how easy it is to forget to handle number parsing errors, or file not founds, etc. making software crash and programmers say “oh, I just forgot that!” or “Really users are so stupid to put ‘blah blah’ in the field named age?”
Hope everyone agrees that if your method throws unchecked exceptions you should tell users that you are doing that, right? But how? Javadocs? No programmers keep updated javadocs. This is why is much better to have self explaining code, than commented code. Checked exception are self explaining code.
But, as I like people smiling :-), I was asking myself if it is possible a workaround to make programmers that prefer unchecked exceptions be happy. Something like an option, a directive, or better an annotation interpreted by the compiler that dynamically adds throws declarations on methods in compile time. This would maintain bytecode compatibility and tools like IDE and Javadoc can continue notifying users that a method throws exceptions.
For example I think javax.rmi.RemoteException would have been unchecked (personally I don’t agree with reasons for which it isn’t)… so I’d like to be able to specify on my code that I don’t want to handle that exceptions. You simply add to your class/methods an annotation that tells the compiler to automatically add throws declaration for RemoteException and its subexceptions. You could define an interface like RemoteExceptionUnaware with such kind of annotation, and implementing that will make your code to automatically throw RemoteExceptions on all the methods needing that.
Good exception handling, now, is to have them checked for that situations that commonly can be handled and recovered. And unchecked for those unrecoverable errors that relies on system failure (hardware/network failure) or bugs (NullPointerException, ClassCastException, etc). Good code let them to reach the point in which the user is notified with a dialog that something went wrong, and you can’t do anything.
2 Responses to “The checked vs unchecked exceptions never ending wars”
- 1 Pingback on Nov 17th, 2008 at 10:52
Leave a Reply
Search
Calendar
| M | T | W | T | F | S | S |
|---|---|---|---|---|---|---|
| « May | Jul » | |||||
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | |
Follow me
Archives
Categories
- Android (3)
- Apple (29)
- Books (7)
- Eclipse (14)
- Errors (5)
- Firefox (7)
- Git (3)
- Hardware (18)
- Horror Code (8)
- Internet (21)
- Java (104)
- JavaScript (9)
- Life, universe and everything (45)
- Lifehacks (26)
- Linux (52)
- Opinions (26)
- OSX (11)
- OWNER API (2)
- Python (1)
- Software (33)
- Speeches and Conferences (8)
- Unix (5)
- Web (23)
- Windows (19)
Tag Cloud
Android apple architecture Bash configuration CSS Development Düsseldorf Eclipse Git Google Hardware hdr How-To howto Java JAXB job Karmic Linux lion MacBook music Open Source Opinion OSX os x patterns Pitfalls Practices Resume Security Software Suspend TDD Testing tip tonemapped Tricks Ubuntu unix video Web Workaround XML
WP Cumulus Flash tag cloud by Roy Tanck and Luke Morton requires Flash Player 9 or better.
Blog License
Blogs I like
Books on the desk
Friends' Blogs
- Antonio Terreno & Valter Bernardini
- Bruno Bossola
- Daniele Galluccio
- Domenico Ventura
- Ed Schepis
- Fabrizio Gianneschi
- Luca Grulla
- Luigi Zanderighi
- Marcello Teodori
- Mida Boghetich
- Muralidharan Chandrasekaran
- Piero Ricca
- Renzo Borgatti
- Simone Bordet
- Simone Bruno
- Uberto Barbini
- Valvolog
- Webtide blogs (Greg Wilkins & Jan Bartel)
Links


















Checked exceptions work really well for code that doesn’t change much or call out to code that can change.
Adding a checked exception to a method can be viral – it affects the callers of that method, and even their callers. If the caller doesn’t want to handle the exception, they may catch and wrap it, but that just obscures the original point. If you and I communicate through a third party, and you know I may throw a FileNotFoundException, the third party should be capable of passing that on to you without wrapping it up.
method1() throws FileNotFoundException {}
method2(){method1();}
method3(){try{method2();}catch(FileNotFoundException e){}}
The above should work, but this is what really happens:
method1() throws FileNotFoundException {}
method2(){try{method1();}catch(FileNotFoundException e){throw new RuntimeException(e);}}
method3(){try{method2();}catch(RuntimeException e){if (e.getCause() instanceof FileNotFoundException){whatever}}}
Much more obscure. Sure, when development is ‘finished’, you can go through and add in the viral throws decorations, but when development is finished, a project is usually dead. Checked exceptions sometimes help, but they often slow you down and lead you to writing poor code like the above. I cover an alternative here: http://rickyclarkson.blogspot.com/2007/05/optional-checked-exceptions-spitting-at.html