尝试块总是昂贵的

尝试块总是昂贵的

本文介绍了尝试块总是昂贵的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,大家,
只是一个关于try..catch块的快速问题。我听说他们使用昂贵,不应该被用作程序流程的一部分。但是,为了验证电子邮件地址,我使用以下代码。

Hey everyone, Just a quick question about try..catch blocks. I've heard they're expensive to use and shouldn't be used as part of a program's flow. However, in order to validate email addresses, I'm using the following code.

        try
        {
            MailAddress checkEmail = new MailAddress(testEmail);

            return true;
        }
        catch
        {
            return false;
        }

由于事先验证,我没有很多异常被捕获,除非它是试图绕过验证。我的问题是,尝试...如果捕获异常,捕获块只有昂贵,或者是否总是昂贵,而不管是否有异常被抛出?

Due to prior validation, I don't many exceptions to be caught unless it's an attempt to bypass validation. My question is, are Try...Catch blocks only expensive if an exception is caught, or is it always expensive regardless of whether any exception is thrown?

谢谢

编辑:感谢所有回复。我决定,由于检查(在C#)不是很昂贵,我会坚持这种方法。这主要是因为抛出一个实际的异常是罕见的,因为先前的验证步骤确保没有人偶然地输入一个无效的电子邮件地址。

EDIT : Thanks for all the replies. I've decided that since the checking (in C#) isn't very expensive, I'll stick with this method. It's mainly because an actual exception being thrown is rare since there are prior validation steps that ensure no one accidentally enters an invalid email address.

推荐答案

一般中,在今天的实现中,输入 try 块并不昂贵(这并不总是如此)。但是,抛出和处理异常通常是一个比较昂贵的操作。因此,例外通常应用于特殊事件,而不是正常的流量控制。

In general, in today's implementations, entering a try block is not expensive at all (this was not always true). However, throwing and handling an exception is usually a relatively expensive operation. So, exceptions should normally be used for exceptional events, not normal flow control.

虽然性能只是一个考虑因素,现代世界。如果(例如)您正在做一些响应用户操作的操作,则从性能的角度来看,即使您可以执行主动检查也可以使用异常,因为异常发生得很快用户不会被打扰。¹但是,如果您正在紧张的循环中执行数十万次操作,或者正在编写可能需要处理巨大负载的Web应用程序,那么您可能会想避免在正常情况下使用异常。

Performance is only one factor to consider, though, especially in the modern world. If (for instance) you're doing something once in response to a user action, it probably doesn't matter from a performance standpoint whether you use an exception even when you could have done a proactive check instead, provided the exception happens quickly enough the user isn't jolted.¹ But if you're doing something in a tight loop that's going to run hundreds of thousands of times, or you're writing a web application that may need to handle a huge load, you'd probably want to avoid using an exception for a normal case.

¹十多年前,我负责对第一个异常抛出的.Net 1.1无触摸部署应用程序完全需要三秒钟。在一个用例中,这是一个足够的问题,涉及打开用户已经询问的文件,这可能合理地不在那里,在尝试打开文件之前,我不得不添加一个文件存在的检查,这通常是糟糕的编程实践尝试打开文件并处理异常,如果失败),纯粹是因为用户体验等待异常来构建是如此糟糕。但这可能不是我们现在生活的世界。

¹ More than a decade ago I was responsible for enhancements to a .Net 1.1 "no touch deployment" application in which the first exception thrown took fully three seconds. This was a sufficient problem in one use case involving opening a file the user had asked for which might reasonably not be there that I had to add a check for file existence prior to trying to open the file, which is normally poor programming practice (just try to open the file and handle the exception if that fails), purely because the user experience waiting for that exception to get built was so poor. But that's probably not the world we live in now.

这篇关于尝试块总是昂贵的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:59