Recommend a design pattern/approach to exposing/tolerating/recovering from system errors, Exception handling (Java, C++, Perl, PHP)
Can you recommend a design pattern/approach to exposing/tolerating/recovering from system errors, Exception handling (Java, C++, Perl, PHP)?
Some errors need to be reported.
Some errors can be handled internally (by a retry or are inconsequential (can be ignored).
How do you structure the code开发者_JAVA技巧 to catch them?
But all errors need to be logged.
What best practises are there?
And for simulating them to be able to fully test components that are impacted by them?
General non-programming-language specific question applicable to several modern programming languages but would welcome example illustrations of patterns, approaches and philosophies in Java, C++, PHP and Perl.
My philosophy involves always attempting to catch errors where they happen. Things like global exception handlers make for a difficult debugging process. Keep error handling as local to the site of the failure as possible. Debugging is easier, and it avoids non-specific error reporting that tends to confuse and frustrate users.
In terms of logging, that's an implementation decision. I usually have some reporting utility functions sitting around that I invoke whenever I detect an error condition. Obviously you can vary the logging level such that certain serious errors are reported. Similarly, if your application should exit with an error, do that as quickly as possible.
Simulation and testing are somewhat orthogonal. To test your error-handling code completely, you should be able to write test cases that cause each error to occur. It may involve contriving inputs or altering state. If you errors are not dependent on inputs or interaction with external systems, then you should really consider whether that's a failure condition, or just an implementation bug that is sporadically triggered.
精彩评论