如何抛出好的异常? [英] How to throw good exceptions?

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

问题描述

我听说你不应该抛出一个字符串,因为缺乏信息,你会捕获你不期望捕获的异常。什么是抛出异常的好办法?你继承一个基类异常类吗?你有很多例外吗?你做MyExceptionClass&或const MyExceptionClass& ?等等。我知道异常不应该抛出析构函数

I heard you should never throw a string because there is a lack of information and you'll catch exceptions you dont expect to catch. What are good practice for throwing exceptions? do you inherit a base exception class? Do you have many exceptions or few? do you do MyExceptionClass& or const MyExceptionClass& ? etc. Also i know exceptions should never been thrown in destructors

我会补充说,我理解设计的合同和什么时候抛出异常。我想问我应该如何抛出异常。

i'll add that i understand design by contract and when to throw exception. I am asking how i should throw exceptions.

推荐答案

在我看来,一个函数应该抛出异常,其承诺,如果它必须打破其合同。函数的签名(名称和参数)决定了它的合同。

In my opinion, a function should throw an exception if it can't keep its "promise", if it has to break its "contract". The function's signature (name and parameters) determine its contract.

给定这两个成员函数:

const Apple* FindApple(const wchar_t* name) const;
const Apple& GetApple(const wchar_t* name) const;

这些函数的名称及其返回值表明在 FindApple ,当没有找到正确的苹果时,该函数完全能够返回NULL,但是在 GetApple 的情况下,你期望一个苹果返回。如果第二个函数不能保证它的promise,它必须抛出一个异常。

The names of these functions as well as their return values indicate to me that in the case of FindApple the function is perfectly capable of returning NULL when the correct apple was not found, but in the case of GetApple you're expecting an apple to return. If that second function can't keep its promise, it must throw an exception.

异常意味着那些异常条件,其中函数没有其他方式报告条件。如果你决定使它成为promise的一部分(read:function signature),那么它可以报告该条件而不抛出异常。

Exceptions are meant for those exceptional conditions in which a function has no other way of reporting these conditions. If you decide to make it a part of the promise (read: function signature) then it can report that condition without throwing an exception.

注意, strong> FindApple ,由调用方决定如何处理找不到正确的苹果的条件,因为它不再是一个特殊条件。

Note that in the case of FindApple, it's up to the caller to decide how to handle the condition of "not finding the right apple" because it's no longer an exceptional condition.

你可能会试图避免所有的异常,但这意味着你必须考虑所有可能的异常条件,而你把负担放在调用者上。调用者需要检查错误条件。

You might be tempted to try to avoid all exceptions, but that means you have to account for all possible exceptional conditions, and you're placing the burden on the caller instead. The caller needs to check for "error conditions" then.

最后,需要处理一个异常,但只有调用者知道如何处理以有用的方式。我的意思是这个在最广泛的可能的解释:一个放弃的服务将稍后再试,一个UI,提供一个有用的错误消息,一个网络应用程序呈现一个oops屏幕,但恢复良好,等等。

Ultimately, an exception needs to be handled, but only by the caller that knows how to handle a particular condition in a useful way. And I mean this in the widest possible interpretation: a service that gives up will try again later, a UI that provides a helpful error message, a web app that presents a "oops" screen but that recovers nicely, ... and so on.

Dave

这篇关于如何抛出好的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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