何时在域类中使用对异常的断言 [英] When to use assertion over exceptions in domain classes

查看:140
本文介绍了何时在域类中使用对异常的断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

使用异常参数验证和其他检查,验证您的类的用户是否按照预期使用它们。



使用断言进行内部一致性检查,即指示因为,如果您的类的用户看到断言失败,他们知道这是(可能)内部错误你的代码,而不是使用你的代码。另一方面,如果获取参数验证异常,他们知道这是他们的错。


Are there any situations when you would use assertion instead of exceptions-handling inside domain classes...

解决方案

Use exceptions for parameter validation and other checks which verify that the users of you classes use them as intended.

Use assertions for internal consistency checks, i.e. to indicate you screwed up, not the user of your class.

Thus, if users of your class see an assertion failure, they know it is (probably) an internal error in your code, not in their use of your code. On the other hand, if the get parameter validation exception, they know it's their fault.

这篇关于何时在域类中使用对异常的断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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