4 ms·
The author notably tries to refute arguments against the use of exceptions for control flow, but doesn't present any reasons (at least not in this article) as t
by TheMiller 14y ago
The author notably tries to refute arguments against the use of exceptions for control flow, but doesn't present any reasons (at least not in this article) as to why this usage might be a good idea.
There's no compelling reason to introduce odd, non-idiomatic code patterns when they don't confer a significant benefit. Some people here argue that the "intent" of exceptions in a language design is not relevant compared to what can be done with them. In general, I disagree with this. Language designers' intent informs the expectations of readers of code, and your readers' expectations are important. Code is a form of writing for communications, both with the computer and with maintainers; it's not artistic writing, where you might deliberately violate expectations or norms in order to delight or surprise your audience.
That said, using exceptions as a method of transferring control to a non-local, dynamically-bound location is a powerful technique for which few languages offer an explicit alternative. It's also difficult for readers to analyze, so it should be used sparingly. For instance, I can think of reasons that one might use this technique within a container or algorithm library, but I can't think of a good reason for a container library to force this control flow idiom on callers by throwing exceptions to signal common situations.
(As usual, Common Lisp is unusual here. It has a throw/catch feature which is explicitly a flow control mechanism, and not just an error-handling mechanism. The standard documentation is interesting in that the Notes section suggests exactly when one would want to use this mechanism:
http://www.lispworks.com/documentation/HyperSpec/Body/s_throw.htm http://www.lispworks.com/documentation/HyperSpec/Body/s_thro...)
- DRMacIver 14y agoGiven that the article was a response to criticism of a previous proposal of mine on grounds that it used exceptions for control flow, and given that I linked to that discussion (on the page for the proposal in question) in the opening paragraphs of this one I probably felt that the reasons for using exceptions for control had been adequately covered at that point. But I was probably not trying to suggest you should regularly do this (certainly I wouldn't suggest that now), only that if you find you have a good reason to do it you shouldn't be afraid of it.
- TheMiller 14y agoSorry I didn't follow the link. Given the link's text, "I have been invited", I didn't expect it to lead to background information, but rather just to, well, an invitation. My bad.
- DRMacIver 14y agoGiven that the article was a response to criticism of a previous proposal of mine on grounds that it used exceptions for control flow, and given that I linked to that discussion (on the page for the proposal in question) in the opening paragraphs of this one I probably felt that the reasons for using exceptions for control had been adequately covered at that point. But I was probably not trying to suggest you should regularly do this (certainly I wouldn't suggest that now), only that if you find you have a good reason to do it you shouldn't be afraid of it.
- DRMacIver 14y agoGiven that the article was a response to criticism of a previous proposal of mine on grounds that it used exceptions for control flow, and given that I linked to that discussion (on the page for the proposal in question) in the opening paragraphs of this one I probably felt that the reasons for using exceptions for control had been adequately covered at that point. But I was probably not trying to suggest you should regularly do this (certainly I wouldn't suggest that now), only that if you find you have a good reason to do it you shouldn't be afraid of it.