tag:blogger.com,1999:blog-347891252975163541.post7437737510588109302..comments2023-05-02T05:20:29.838-07:00Comments on On Coding Style: ExceptionsAndrew Minerhttp://www.blogger.com/profile/02245034550316893904noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-347891252975163541.post-10969229529507462402009-09-15T21:27:33.211-07:002009-09-15T21:27:33.211-07:00Oops... I meant: "...the most graceful thing ...Oops... I meant: "...the most graceful thing to do is *often to* raise a..."Andrew Minerhttps://www.blogger.com/profile/02245034550316893904noreply@blogger.comtag:blogger.com,1999:blog-347891252975163541.post-85276544448814783522009-09-15T21:24:47.963-07:002009-09-15T21:24:47.963-07:00I agree with Tom that having language-level suppor...I agree with Tom that having language-level support for exceptions is not a substitute for writing rigorous, defensive code. On the contrary, I think using exceptions (i.e., throwing and catching them appropriately) is a major boon to writing robust and correct code. Once a programmer has "anticipate[d] all the various ways in which their code can be misused", the most graceful thing to do is raise a well-choosen exception to allow a component with more context of the actual user intent to gracefully clean up and deal with the problem in a friendly way.Andrew Minerhttps://www.blogger.com/profile/02245034550316893904noreply@blogger.comtag:blogger.com,1999:blog-347891252975163541.post-33025005656186333852009-09-15T19:52:28.128-07:002009-09-15T19:52:28.128-07:00In my experience, programmers tend to rely on exce...In my experience, programmers tend to rely on exceptions to alleviate themselves of the responsibility of writing robust code. For example, a square root function which doesn't bother to check if the input it was passed is a negative number.<br /><br />Ideally, programmers should anticipate all the various ways in which their code can be misused, and handle them as gracefully as possible. Doing so can add more functionality in the code and provide the user with a more productive experience.Tom Lahtinoreply@blogger.comtag:blogger.com,1999:blog-347891252975163541.post-89199123625273744632009-05-05T13:55:00.000-07:002009-05-05T13:55:00.000-07:00In my opinion exceptions should not be used to pas...In my opinion exceptions should not be used to pass information except that which is necessary to identify the immediate cause of the exception, e.g., stack trace plus whatever variables had an invalid value etc. Exceptions should not be seen as a pipe for passing extra information, because if that information really *needs* to travel to some destination there should have been an explicit code path for it to travel. One serious potential side effect of extra info in an exception is that if the exception is NOT handled the way you expect, the info in the exception may be exposed to the user, which may be a security risk or privacy issue.Brad Williamshttps://www.blogger.com/profile/09839532299808900672noreply@blogger.comtag:blogger.com,1999:blog-347891252975163541.post-46950301994942531132008-12-08T05:17:00.000-08:002008-12-08T05:17:00.000-08:00Exceptions should also be self-describing and logg...Exceptions should also be self-describing and logged, using whatever mechanism your language supports (toString(), ex.what(), etc.). It's frustrating to see an exception thrown in a production environment which is hard to produce but gives you nothing more than some very basic information, like the exception class name.<BR/><BR/>Any exception should give as much information as possible. And, since you noted exceptions can hold data, this should usually be possible.Anonymousnoreply@blogger.com