学习异常处理模式 [英] Learning Exception Handling Patterns

查看:118
本文介绍了学习异常处理模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编程中一直使我感到神秘的一件事是使用适当的异常处理。 Code Complete指出,通常90%的代码集中在处理异常。虽然我知道实施一个基本异常的基础知识,但我没有为这样一个重要的主题找到很好的一般资源。



我正在寻找好的资源(不一定与一种特定语言),用于学习如何实现良好的异常处理技术。大多数关于stackoverflow的异常处理主题似乎集中在特定语言的特定情况。您的建议是什么?

解决方案

多年来,我一直是安全关键系统的设计师和编码人员,这种类型的系统是鲁棒性,其中一个方面是异常处理。



一些基本的最佳做法:



1)在任何层面上,只捕捉那些可以处理的例外。 处理几乎总是意味着重试或放弃你想要做的事情,这通常意味着你应该从发出异常抛出电话的一级开始捕获异常。



2)捕获API文档中列出的异常,不再更多。即使列出了五个异常,请不要抓住它们的共同基类(如果有的话)。但是,您并不总是需要在同一个地方从同一个电话中捕获所有异常。



3)不要将严格的异常传递到一个级别在那里他们没有传达有意义相反,在低级别捕获异常,如果适用,请记录它,并返回一个状态代码或者向上述调用者抛出一个更通用的异常。



4)只使用(并且使用)当你在一个关键的部分时,所有的处理程序。捕获异常,放弃资源(信号量或其他),重新启动异常。



5)记录异常不处理它。如果您在特定处理程序中执行的所有操作都是记录异常,则应该可以摆脱该处理程序。



6)对于循环中的异常处理程序,尤其是循环如果抛出异常,没有延迟或出路。你可以很容易地以这种方式陷入困境。


One thing that has always mystified me in programming is the use of appropriate exception handling. Code Complete points out that often times 90% of code is focused on handling exceptions. While I know the basics of implementing a basic exception, I have not found good general resources for such an important topic.

I am looking for good resources (not necessarily tied to a specific language) for learning how to implement good Exception Handling techniques. Most of the Exception Handling topics on stackoverflow seem to focus on a specific situation in a specific language. What would be your recommendations?

解决方案

I have been a designer and coder of safety-critical systems for many years, and key to that type of system is robustness, one aspect of which is exception handling.

Some basic best practices:

1) At any level, catch only those exceptions you can deal with then and there. To "deal with" almost always means to retry or give up what you were trying to do, and that usually means that you should catch the exception one level up from where the exception-throwing call is issued.

2) Catch exactly those exceptions listed by the API documentation, no more, no less. Even if there are five exceptions listed for a call, don't catch their common base class (if they have one). However, you don't always need to catch all exceptions from the same call in the same place.

3) Don't pass nitty-gritty exceptions up to a level where they carry no meaningful information. Instead, catch the exception at the low level, log it if appropriate and return a status code or throw a more generic exception to the caller above.

4) Only use (and do use) catch-all handlers when you are in a critical section. Catch the exception, relinquish the resource (semaphore or whatever), rethrow the exception.

5) Logging an exception is not handling it. If all you do in a particular handler is log the exception, you should probably get rid of that handler.

6) Be very careful with exception handlers in loops, especially loops without a delay or a way out if the exception is thrown. You can easily get stuck in a busy loop that way.

这篇关于学习异常处理模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆