作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在我的代码中使用了这样的东西:
//some code that gets the group, etc.
Point3d rotationPoint;
try
{
rotationPoint = GetRotationPoint(group) // Throws NoDataFoundException
}
catch(NoDataFoundException)
{
rotationPoint = RequestRotationPoint(); // Let user pick the rotation point instead
}
//...and then simply continue the method
采用这种方法的原因是我无法检查 rotationPoint
是否为 null
,因为它是一个 struct
。会有替代方案吗?
最佳答案
这是一种不好的做法,但实际上是因为您正在使用异常来处理系统中的逻辑,而不是您在重复类似的操作。异常应该是 Exceptional
,因为您并不真正期待它们,所以您会很好地呈现给用户并尝试继续或优雅地失败。
在这种情况下,您真正想要做的是遵循 TryParse
方法:
Point3d rotationPoint;
if(GetRotationPoint(group, out rotationPoint) == false)
{
rotationPoint = RequestRotationPoint();
}
编辑
我应该补充一点,异常对于做这类事情来说是不好的做法,因为构造和抛出异常是一项昂贵的操作,可能会导致代码出现性能瓶颈。通常这不是您需要担心的事情,但有时它是 - 如果您已经开始沿着这条路 build ,可能很难后退。
关于c# - 继续使用 catch block 中设置的值的不良做法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28968236/
我在使用 Heroku 时遇到问题,当我尝试部署我的应用程序时,它没有运行 run assets:precompile而且我还没有在本地预编译我的 Assets ,我在 Git 中没有 list 文件
(这是为您提供围绕我的问题的背景信息。您可以跳到“问题”并阅读该内容,然后如果您想直接进入主题,则可以返回并浏览背景知识.抱歉,这是一面文字墙!) 我需要将一堆非常非常糟糕的 JSON 存储在数据库中
我是一名优秀的程序员,十分优秀!