C++ 异常与断言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/409794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception vs Assert?
提问by
Possible Duplicate:
design by contract tests by assert or by exception?
可能的重复:
通过断言或异常进行合同测试设计?
Is there a rule of thumb to follow when deciding to use exceptions instead of asserts (or vice versa). Right now I do only throw if its something I think will happen during runtime on the user side (like a socket or file error). Almost everything else I use asserts.
在决定使用异常而不是断言(或反之亦然)时,是否有可遵循的经验法则。现在我只在我认为在用户端运行时会发生的事情时抛出(比如套接字或文件错误)。我使用的几乎所有其他东西都是断言。
Also, if I were to throw an assert, what is a nice standard object to throw? IIRC there is std::logic_error but that is not a good object to throw? what would I throw for a missing file or unexpected input (such as from the command line instead of a frontend app)?
另外,如果我要抛出一个断言,那么要抛出的好标准对象是什么?IIRC 有 std::logic_error 但这不是抛出的好对象吗?对于丢失的文件或意外输入(例如来自命令行而不是前端应用程序),我会抛出什么?
回答by Mike Hofer
My rule of thumb:
我的经验法则:
Exceptions are used for run-time error conditions (IO errors, out of memory, can't get a database connection, etc.).
异常用于运行时错误条件(IO 错误、内存不足、无法获得数据库连接等)。
Assertions are used for coding errors (this method doesn't accept nulls, and the developer passed one anyway).
断言用于编码错误(此方法不接受空值,开发人员无论如何都传递了一个)。
For libraries with public classes, throw exceptions on the public methods (because it makes sense to do so). Assertions are used to catch YOUR mistakes, not theirs.
对于具有公共类的库,在公共方法上抛出异常(因为这样做是有意义的)。断言用于捕捉您的错误,而不是他们的错误。
EDIT: This may not be entirely clear, due to the null value example. My point is that you use assertions (as others have pointed out) for conditions that should NEVER happen, for conditions that should NEVER make it into production code. These conditions absolutely must fail during unit testing or QA testing.
编辑:由于空值示例,这可能并不完全清楚。我的观点是,您将断言(正如其他人指出的那样)用于永远不会发生的条件,以及永远不会将其纳入生产代码的条件。在单元测试或 QA 测试期间,这些条件绝对必须失败。
回答by Mike Hofer
Assert the stuff that you knowcannot happen (i.e. if it happens, it's your fault for being incompetent).
断言你知道不会发生的事情(即如果发生了,那是你无能的错)。
Raise exceptional situations which are not treated by the regular control flow of the program.
引发程序的常规控制流未处理的异常情况。
回答by Toon Krijthe
You use exceptions for exceptionalsituations. For example an out of memory situation or a network failure.
您在特殊情况下使用异常。例如内存不足或网络故障。
You use assert to ascertainthat a cetain precondition is met. For example a pointer is not NULL or an integer is within a certain range.
您可以使用 assert 来确定是否满足某个先决条件。例如,指针不为 NULL 或整数在某个范围内。
回答by Paul Beckingham
I use asserts for things that should never happen, yet do. The sort of thing that when it happens, the developer needs to revisit incorrect assumptions.
我对不应该发生的事情使用断言,但会发生。当它发生时,开发人员需要重新审视不正确的假设。
I use exceptions for everything else.
我对其他一切都使用异常。
In reusable code, I prefer an exception because it gives the caller a choice of handling or not handling the problem. Just try catching and handling an assert!
在可重用代码中,我更喜欢异常,因为它让调用者可以选择处理或不处理问题。只需尝试捕获和处理断言!
回答by Loki
Assert is a means to verify that the program is in a possible state. If a function returns -1 when it should only return positive integers, and you have an assert that verifies that, your program should stop because it puts your program in a dangerous state.
断言是一种验证程序是否处于可能状态的手段。如果一个函数在它应该只返回正整数时返回 -1,并且您有一个断言来验证这一点,那么您的程序应该停止,因为它使您的程序处于危险状态。
回答by donpark
As a general rule, I throw exceptions from:
作为一般规则,我抛出以下异常:
- public functions of a package to catch programming errors.
- internal functions to report system errors or pass-through sub-system errors.
- 用于捕获编程错误的包的公共函数。
- 报告系统错误或传递子系统错误的内部函数。
where I use asserts only internally to catch implementation mistakes.
我只在内部使用断言来捕捉实现错误。