指导您的异常处理政策的原则是什么? [英] What are the principles guiding your exception handling policy?

查看:154
本文介绍了指导您的异常处理政策的原则是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理异常时涉及很多相关性。除了低级别API之外,例外情况涵盖硬件和操作系统提供的错误,程序员确定构成异常的一个阴暗区域,什么是正常条件。



您如何决定何时使用异常?

解决方案

异常应用作在对象内部的方法之间传递信息的方法,在本地,您应该使用错误代码和防御性编程。



异常旨在从一个点传递控件其中检测到错误可以处理错误的地方(较高的堆栈)的错误,可能是因为本地代码没有足够的上下文来纠正问题,并且更高的堆栈将具有更多的上下文,因此能够更好地组织恢复。



在考虑异常时(至少在C ++中),您应该考虑API提供的异常保证。最低限度的保证应该是基本的保证,虽然你应该努力(在适当的地方)提供强有力的保证。在不使用关键API的外部依赖关系的情况下,甚至可以尝试提供无抛出保证。



NB 不要混淆异常保证有异常规格。



例外保证:



不保证:




在异常转义方法后,对象的状态不保证
在这种情况下,不再需要使用对象。




基本保证:




几乎在所有情况下,这应该是一个方法提供的最低保证。
这保证了对象的状态是很好的定义,仍然可以一直使用。




强大的保证:(又名事务保证)




这保证方法将完全成功
或抛出异常,对象状态不会改变。 p>



无提示保证:




方法保证允许异常传播出该方法。
所有的析构函数都应该保证。

|注:如果一个异常转义了析构函数,而异常已在传播

|应用程序将终止



There is a lot of relativity involved in working with exceptions. Beyond low level APIs where exceptions cover errors raised from hardware and the OS there is a shady area where the programmer decides what constitutes an exception and what is a normal condition.

How do you decide when to use exceptions? Do you have a consistent policy regarding exceptions?

解决方案

Exceptions should not be used as a method of passing information internally between methods inside your object, locally you should use error codes and defensive programming.

Exceptions are designed to pass control from a point where an error is detected to a place (higher up the stack) where the error can be handled, presumably because the local code does not have enough context to correct the problem and something higher up the stack will have more context and thus be able to better organize a recovery.

When considering exceptions (in C++ at least) you should consider the exception guarantees that your API makes. The minimum level of guarantee should be the Basic guarantee though you should strive (where appropriate) to provide the strong guarantee. In cases where you use no external dependencies from a articular API you may even try to provide the no throw guarantee.

N.B.Do not confuse exception guarantees with exception specifications.

Exception Guarantees:

No Guarantee:

There is no guarantee about the state of the object after an exception escapes a method In these situations the object should no longer be used.

Basic Guarantee:

In nearly all situations this should be the minimum guarantee a method provides. This guarantees the object's state is well defined and can still be consistently used.

Strong Guarantee: (aka Transactional Guarantee)

This guarantees that the method will completely successfully Or an Exception will be thrown and the objects state will not change.

No Throw Guarantee:

The method guarantees that no exceptions are allowed to propagate out of the method. All destructors should make this guarantee.
| N.B. If an exception escapes a destructor while an exception is already propagating
| the application will terminate

这篇关于指导您的异常处理政策的原则是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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