C++库研究笔记——使用assert还是exception?

本文探讨了在软件开发中使用断言(assert)与异常(exception)的区别。总结指出,在检查内部系统的函数或数据时应使用断言,而在验证公共API的前置条件或处理外部数据时应使用异常。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

design by contract tests by assert or by exception?

今天也在考虑这个问题,google了下,投票最多的是:

The rule of thumb is that you should use assertions when you are trying to catch your own errors, and exceptions when trying to catch other people's errors. In other words, you should use exceptions to check the preconditions for the public API functions, and whenever you get any data that are external to your system. You should use asserts for the functions or data that are internal to your system.

总结很精辟。


另一个是:

Exception vs Assert? [duplicate]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值