属性和异常 [英] Properties and Exceptions

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

问题描述

假设我有一个双属性,我知道允许的唯一值是0到1,访问权限是公共的,但是设置是私有的. 如果将该属性设置为0到1以外的任何值,还是应该将其设置为异常,是因为不是由外部源设置该属性,我应该抛出异常吗?

Let's say I have a double property and I know the only values allowed are from 0 to 1, the access is public, however, the set is private. Should I throw an exception if the property is being set to anything other than 0 to 1 or leave it be because it isn't being set by an external source?

推荐答案

如果它实际上只能是0或1,那么无论集合来自何处,都可能会出现异常.发生异常时可以捕获异常,并且在课堂内外都可能出错.

If it can really only be 0 or 1, then an exception is probably in order, no matter where the set comes from. An exception is to catch when something has gone wrong, and things can go wrong inside the class as well as outside.

顺便说一句,在这种情况下,我会使用布尔值,这样就不会有任何错误.

And by the way, I would use a bool in that situation so there can't possibly be any mistake.


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

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