它是"坏QUOT;使用的try-catch在.NET流量控制? [英] Is it "bad" to use try-catch for flow control in .NET?

查看:132
本文介绍了它是"坏QUOT;使用的try-catch在.NET流量控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在一个项目中找到:

I just found in a project:

try
{
    myLabel.Text = school.SchoolName;
}
catch
{
    myPanel.Visible = false;
}

我要说话,而不是写这个开发商,他说,招致空异常(因为理论上可能为空,而不是 myLabel )将无形中使计算机发出三次蜂鸣声和睡眠两秒钟。但是,我不知道如果我记错有关规则。显然,这不是try / catch语句的用途,但是这是不好的,因为这违背本意,还是不好的,因为性能方面的考虑?我觉得这只是坏,但我想多说不是这是非常糟糕的。

I want to talk to the developer than wrote this, saying that incurring the null exception (because school might theoretically be null, not myLabel) would virtually make the computer beep three times and sleep for two seconds. However, I wonder if I'm misremembering the rule about that. Obviously, this isn't the intended use for try/catch, but is this bad because it defies intention, or bad because of performance considerations? I feel like it's just bad, but I want to say more than "that's really bad".

推荐答案

只是因为它是坏的设计,你不应该使用控制流异常。它没有意义。例外情况是例外的情况,而不是正常的流动。性能可能不会在这种情况下的一个问题,因为在现代硬件上最先进的应用程序,你可以整天抛出异常,用户不会注意到性能的下降。但是,如果这是一个高性能的应用程序处理大量的数据,或做了很多的一些工作的,那么,业绩将是一个问题。

You should not use exceptions for control flow simply because it is bad design. It doesn't make sense. Exceptions are for exceptional cases, not for normal flow. Performance probably won't be an issue in this situation because for most modern applications on modern hardware, you could throw exceptions all day long and the user wouldn't notice a performance hit. However, if this is a high performance application processing a lot of data or doing a lot of some sort of work, then yes, performance would be a concern.

这篇关于它是"坏QUOT;使用的try-catch在.NET流量控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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