Can someone cite a good reference for programming with exceptions?
I prefer to have the "rc" error-code return style of error management. I agree that this presents challenges that are better served by throw-catch, however, I still feel that I am not designing and implementing in a style that is clean and maintainable. So, I am looking for a good book that discusses the pattern and is not simply a reference book.
A bibliography of references would be fine too ...
An excerpt from the answer below, "Practices of an Agile Programmer", that I found especially striking:
**Keeping Your Balance**
• Determining who is responsible for handling an exception is part of design.
• Not all situations are exceptional.
• Report an exception that has meaning in the context of this code. A NullPointerExc开发者_运维知识库eption is pretty but just as useless as the null object described earlier.
• If the code writes a running debug log, issue a log message when an exception is caught or thrown; this will make tracking them down much easier.
• Checked exceptions can be onerous to work with. No one wants to call a method that throws thirty-one different checked exceptions. That’s a design error: fix it, don’t patch over it.
• Propagate what you can’t handle.
I have not found a book dedicated to exception handling, but there are some which deal with this topic at the length of a section or chapter.
As a primer and for a language agnostic approach see [Martin, Ch. 7]. [McConnel, Ch. 8.4] also deals with exception handling on a very general basis. For addtionally good advices for the use of exceptions see [Subramaniam, Hunt, Ch. 36, 37]. I also found [Richter, Ch. 20] very useful although it is specific to .NET and C#. Nevertheless some sections are applicable to other languages, too.
Recommendation: As an alternative approach to throwing exceptions and error-code return style of programming, do some research on the "Special Case Pattern" or "Null Object Pattern" in the WWW.
- [Martin] Martin, C. R. (2008). Clean Code: A Handbook of Agile Software Craftsmanship. Prentice Hall International.
- [McConnel] McConnel, S. (2004). Code Complete.
- [Subramaniam, Hunt] Subramaniam, V., & Hunt, A. (2006). Practices of an Agile Developer. Pragmatic Programmers.
- [Richter] Richter, J. (2010). CLR via C#. Microsoft Press.
I'm not aware of any book dealing specifically with best practices of exception throwing/usage. It may be too specific a topic to warrant an entire book. Also, best practices are likely to be vary significantly for each programming language (C++ vs. Java), as well as programming contexts within a language (C# winforms vs. C# asp.net).
I suspect your best bet is to google it; you'll likely find some blog posts that address the topic (hopefully, the best/most-respected ones will be near to top of google results).
精彩评论