Showing posts with label bruce eckels. Show all posts
Showing posts with label bruce eckels. Show all posts

Monday, March 24, 2003

Checked vs Unchecked

Another posting from Bruce Eckels describes his continued support of unchecked exceptions. His disturbing article, "Does Java need Checked Exceptions?, has bothered me for a very long time. I've found checked exceptions, especially typed ones (see Bill de hÓra counter discussion), the most useful thing for tracking and handling errors. The thing with Runtime exceptions is that they allow the responsibility for handling the errors to be lost and somewhere in a big framework the context can go missing (especially with RMI being involved).

Speaking of RMI, the best example I've seen as to why RMIException is checked and not runtime came from a Sun engineer:

"3) checked exceptions foster more robust programs

There was a time when Oak and the earliest version of Java did not have checked exceptions. Exception handling was advisory, and it was an unsafe world out there. It was our group (Jim Waldo and me in particular :-) that recommended that there be exceptions checked by the compiler. Jim was quite persuasive in his arguments, telling of a world where robust code would reign. After some consideration, Java was retooled to have checked exceptions. Only those exceptions for which there was no recovery or reflect application errors would be unchecked (e.g., OutOfMemoryError, NullPointerException respectively). And the world was safe again.

Imagine the Java engineers' surprise when many exceptions in the Java API and compiler were changed from unchecked to checked, and the compiler enforced the distinction, they uncovered bugs in the implementations! So, the best efforts at handling error conditions, however good intentioned, was not good enough. That compiler is useful for something :-)"