Ted Leung on the air
Ted Leung on the air: Open Source, Java, Python, and ...
Ted Leung on the air: Open Source, Java, Python, and ...
Tue, 19 Aug 2003
Hjelsberg on Checked Exceptions, Java and C#
Bill Venners and Bruce Eckel
interviewed Anders Hjelsberg on the topic of checked exceptions. My contention has long been that checked exceptions are a good thing, but that the way that they've been used in the Java API's is a disaster. These two quotes support that idea.
[00:50] |
[computers/programming/java] |
# |
TB |
F |
G |
6 Comments |
Frankly, they look really great up front, and there's nothing wrong with the idea. I completely agree that checked exceptions are a wonderful feature. It's just that particular implementations can be problematic. By implementing checked exceptions the way it's done in Java, for example, I think you just take one set of problems and trade them for another set of problems. In the end it's not clear to me that you actually make life any easier.Checked exceptions are good.
You see programmers picking up new APIs that have all these throws clauses, and then you see how convoluted their code gets, and you realize the checked exceptions aren't helping them any. It is sort of these dictatorial API designers telling you how to do your exception handling. They should not be doing that.API designers are using them wrongly. So far, no disagreement. The Hjelsberg brings up two areas that I haven't seen discussed before (or at least not from this point of view): the impact of checked exceptions on versioning of classes/interfaces, and the issue of scalablity. The scalability one is particularly convincing for me.
The scalability issue is somewhat related to the versionability issue. In the small, checked exceptions are very enticing. With a little example, you can show that you've actually checked that you caught the FileNotFoundException, and isn't that great? Well, that's fine when you're just calling one API. The trouble begins when you start building big systems where you're talking to four or five different subsystems. Each subsystem throws four to ten exceptions. Now, each time you walk up the ladder of aggregation, you have this exponential hierarchy below you of exceptions you have to deal with. You end up having to declare 40 exceptions that you might throw. And once you aggregate that with another subsystem you've got 80 exceptions in your throws clause. It just balloons out of control.Even if API designers use checked exceptions correctly, the blow up certainly causes problems. But what's the solution here? I like the idea of checked exceptions, but this inability to scale is evident in large Java programs.
C# is basically silent on the checked exceptions issue. Once a better solution is known-and trust me we continue to think about it-we can go back and actually put something in place. I'm a strong believer that if you don't have anything right to say, or anything that moves the art forward, then you'd better just be completely silent and neutral, as opposed to trying to lay out a framework.This seems like wisdom to me. Study the problem some more, get some more practical experience and then add a truly useful feature. I can respect that.
And so, when you take all of these issues, to me it just seems more thinking is needed before we put some kind of checked exceptions mechanism in place for C#. But that said, there's certainly tremendous value in knowing what exceptions can get thrown, and having some sort of tool that checks. I don't think we can construct hard and fast rules down to, it is either a compiler error or not. But I think we can certainly do a lot with analysis tools that detect suspicious code, including uncaught exceptions, and points out those potential holes to you.This is a variant of what I've been saying regarding the static vs dynamic typing issue. Maybe it really does make sense to keep some features like static types and checked exceptions out of the language, and provide better analysis tools that can be run over the code. If these tools became part of the programming environment, then their warnings would be indistinguishable from compiler errors or warnings.
Could my mom design C#?
Posted by Trackback from Scott Watermasysk On .NET at Tue Aug 19 06:10:52 2003
Posted by Trackback from Scott Watermasysk On .NET at Tue Aug 19 06:10:52 2003
Moving from Java to C#, I was pretty offended by the lack of checked exceptions, until I thought about what the consequences of an uncaught exception were, vs. an exception that's caught and eaten. Now I lean more towards catching only the things I know about and letting the rest go (in C#, where I have that option) - right now this relies on the docs being correct about exceptions, which they often aren't. But I think that this is the right way to go; if you can't actually do something, exactly what benefit do you get from an exception handler? Looking at catch clauses, I see one of 3 actions: nothing, logging or displaying an error message, or actually trying to do some corrective action. The latter is far and away the rarest, it seems like in the code I've seen, if an exceptional condition happens, there's hardly anything that can be done without user intervention. In fact, I think that it's something of an anti-pattern do write APIs that can throw an exception for non exceptional conditions: look at the .NET HttpWebRequest classes, which will throw an exception for any HTTP status other than 200. Here's a great example of a case where you can actually do something, like if a document's moved, but it's also awkward to handle this case in a catch block.
Posted by Gordon Weakliem at Tue Aug 19 09:11:33 2003
Posted by Gordon Weakliem at Tue Aug 19 09:11:33 2003
You say: My contention has long been that checked exceptions are a good thing, but that the way that they've been used in the Java API's is a disaster.
If the Java API's are using checked exceptions incorrectly, what is the proper way to use checked exceptions?
Posted by Jim Weirich at Tue Aug 19 11:07:33 2003
If the Java API's are using checked exceptions incorrectly, what is the proper way to use checked exceptions?
Posted by Jim Weirich at Tue Aug 19 11:07:33 2003
Use them for errors that can actually be recovered from. If there isn't a way to recover, throw an unchecked exception.
Posted by Ted Leung at Fri Aug 22 02:30:56 2003
Posted by Ted Leung at Fri Aug 22 02:30:56 2003
There are some things I don't understand in this endless checked/unchecked debate:
- if the problem is with handling exceptions in large systems, the problem still exists if you want to handle unchecked exceptions (you have to deal with a large hierarchy of exceptions).
- if the problem is dealing with new versions of subsystems, assuming you want to handle the exceptions, unchecked exceptions fail to notify you of the change and checked exceptions too (if re-compiling isn't necessary).
Unchecked exceptions don't help the versionning problem (new exceptions could be thrown without you adding the handling code) and they don't help the management of large ranges of exceptions.
If you look at code written from .NET programmers from MS, they usually catch a couple of specific exception and then catch the generic "Exception". Is that what C# programmers should do?
Solution?
A good analysis tool would help with the versionning, but the question remains: when should this check occur? Development time, compile time or runtime?
Handling large ranges of errors generated by large subsystems isn't specific to exceptions, but applies to error handling in general. Checked exceptions don't make the code cleaner (or dirtier), but does helps not forgetting to handle all error conditions. Of course, using "catch(Exception)" bypasses this benefit...
Posted by Dumky at Sat Aug 23 10:45:14 2003
- if the problem is with handling exceptions in large systems, the problem still exists if you want to handle unchecked exceptions (you have to deal with a large hierarchy of exceptions).
- if the problem is dealing with new versions of subsystems, assuming you want to handle the exceptions, unchecked exceptions fail to notify you of the change and checked exceptions too (if re-compiling isn't necessary).
Unchecked exceptions don't help the versionning problem (new exceptions could be thrown without you adding the handling code) and they don't help the management of large ranges of exceptions.
If you look at code written from .NET programmers from MS, they usually catch a couple of specific exception and then catch the generic "Exception". Is that what C# programmers should do?
Solution?
A good analysis tool would help with the versionning, but the question remains: when should this check occur? Development time, compile time or runtime?
Handling large ranges of errors generated by large subsystems isn't specific to exceptions, but applies to error handling in general. Checked exceptions don't make the code cleaner (or dirtier), but does helps not forgetting to handle all error conditions. Of course, using "catch(Exception)" bypasses this benefit...
Posted by Dumky at Sat Aug 23 10:45:14 2003
You wrote:
Use them for errors that can actually be recovered from. If there isn't a way to recover, throw an unchecked exception.
Whether an error is recoverable or not is context-dependent, wouldn't it? Callee (the thrower) does not necessarily know if caller can/should recover from the error or not.
Posted by Dasa at Sat Oct 8 21:33:41 2005
Use them for errors that can actually be recovered from. If there isn't a way to recover, throw an unchecked exception.
Whether an error is recoverable or not is context-dependent, wouldn't it? Callee (the thrower) does not necessarily know if caller can/should recover from the error or not.
Posted by Dasa at Sat Oct 8 21:33:41 2005
You can subscribe to an RSS feed of the comments for this blog:
Add a comment here:
You can use some HTML tags in the comment text:
To insert a URI, just type it -- no need to write an anchor tag.
Allowable html tags are:
You can also use some Wiki style:
URI => [uri title]
<em> => _emphasized text_
<b> => *bold text*
Ordered list => consecutive lines starting spaces and an asterisk
To insert a URI, just type it -- no need to write an anchor tag.
Allowable html tags are:
<a href>
, <em>
, <i>
, <b>
, <blockquote>
, <br/>
, <p>
, <code>
, <pre>
, <cite>
, <sub>
and <sup>
.You can also use some Wiki style:
URI => [uri title]
<em> => _emphasized text_
<b> => *bold text*
Ordered list => consecutive lines starting spaces and an asterisk