断言与异常 [英] Assert vs. exceptions
问题描述
什么时候应该更喜欢断言抛出异常?
When should one prefer assert-ing to throwing an exception?
推荐答案
BigMan写道:
什么时候应该更喜欢断言抛出异常?
When should one prefer assert-ing to throwing an exception?
断言是一种仅用于调试构建的工具。它们不存在于
发布版本中,这通常是支付给
客户的程序。
-
Karl Heinz Buchegger
kb******@gascad.at
2005年4月13日01:10:42 -0700,BigMan< Bi **** @ abv.bg>写道:
On 13 Apr 2005 01:10:42 -0700, BigMan <Bi****@abv.bg> wrote:
什么时候应该更喜欢断言抛出异常?
When should one prefer assert-ing to throwing an exception?
断言应该保护(不总是明显的) )
开发人员的错误,例如使用指针尽管它是NULL。
异常是一种处理可能合理地发生在运行时的错误的方法,例如:试图连接到某个服务器失败(可能由于各种原因而无法回复)。
assertions should protect from (not always obvious) mistakes of the
developer, e.g. using a pointer despite its being NULL.
exceptions are a way to handle errors that may legitimately occur at
runtime, e.g. the failure of trying to connect to some server (which may
not respond for various reasons).
BigMan写道:
BigMan wrote:
什么时候应该更喜欢断言抛出异常?
When should one prefer assert-ing to throwing an exception?
总是断言,除非你别无选择,即便如此,也可以考虑将
作为设计缺陷予以消除。
Always assert, unless you have no other choice, but even then consider
the exception to be a design flaw to be eliminated.
这篇关于断言与异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!