PHP - try/catch 的开销是否比 if/then 更高?
在处理一些遗留代码时,我遇到了大量的 Try/Catch 语句。 Try/Catch 并不是他们在我的 Zend 认证课程中教授的内容,并且在 10 年来我没有与其他使用过它的 PHP 开发人员一起工作。与执行 if 语句相比,Try/Catch 是否有额外的开销?与其他选择相比,什么使它更受欢迎或更不受欢迎?
Working in some legacy code, I've run across a ton of Try/Catch statements. Try/Catch isn't something that they taught in my Zend certification classes, and in 10 years I've not worked with another PHP developer who's used it. Does Try/Catch have extra overhead compared to doing if statements? What would make it more or less desirable than other options?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不认为他们之间有真正的联系。
if 语句用于确定分支逻辑。
Try/Catch 是为了处理发生的错误。可以在 Catch 块中处理会停止程序的异常。
I don't consider them to really be related to each other.
If statements are for determining branching logic.
Try/Catch is to handle errors that occur. Exceptions that would halt the program can be handled in the Catch block.
好吧,如果我理解正确的话,try/catch 块会向堆栈添加一层。所以是的,它可能会存在严重的性能问题。然而,它通过让您在需要时处理错误而提供的好处也很重要。
if
语句的开销非常小。因此,为了直接回答你的问题,是的,try/catch 的开销比 if/then 的开销要高得多(抛出异常的开销要大得多,因为它为每次抛出生成一个回溯)。话虽如此,他们都有自己的目的。例外应该用于特殊情况。您应该使用它们来检测不属于正常故障范围的错误。例如,如果用户没有在注册页面上输入足够长的密码,则不会引发异常。但是,如果无法连接到数据库来执行注册,则会抛出异常。一种是逻辑错误,另一种是需要中断正常程序流程的情况。
Well, if I understand correctly, a try/catch block adds a layer to the stack. So yes, there can be significant performance issues with it. However, the gains that it provides by letting you handle errors where you need to are significant as well. An
if
statement has very little overhead. So to directly answer your question, yes try/catch has a significantly higher overhead than if/then (Throwing exceptions has a LOT more overhead, since it generates a backtrace for each throw).With that said, they both have their purpose. Exceptions should be used for, well, exceptional conditions. You should use them to detect things that went wrong that are not within the normal realm of failure. For example, you wouldn't throw an exception if a user didn't enter a long enough password on the registration page. But you would throw an exception if you couldn't connect to the database to perform the registration. One is a logic error, and the other is a condition that needs to interrupt normal program flow.
Try/catch 用于错误处理。 If 语句是简单的布尔测试器。他们根本不做同样的事情。您应该使用 if 语句并测试您知道的每个条件,但使用 try/catch 进行异常处理。
Try/catch is used for error handling. If statements are simple boolean testers. They don't do the same things at all. You should use if statements and test for each condition you know about, but use try/catch for exception handling.
try/catch
的全部要点在于它是非本地的。您可以一次退出多个循环、打破嵌套函数调用、从您进入的任何地方退出。if
无法做到这一点,而且也无意这样做。我不知道开销,但我强烈且知情地怀疑它的开销远不止于 if。最终,使用适合工作的工具:它们不可互换。好吧,它们是,但它们不应该互换:)
更新:许多其他人说
try/catch
用于错误处理。他们不是。它们用于异常处理。例如,在许多语言中,尝试从迭代器的最后一个元素获取下一个元素将引发异常;这是异常的完全有效的使用。每当发生意外情况时,您都可以使用它们,这些情况必须在当前范围之外进行处理(假设您没有提供回调来处理它)。The whole point of
try/catch
is that it is non-local. You can exit multiple loops at a stroke, break out of nested function calls, escape from anywhere you get into.if
can't do that, and is not meant to. I do not know about the overhead, but I strongly and informedly suspect that it has much more than if. Ultimately, use the tool right for the job: they are not interchangeable.Okay, they are, but they shouldn't be interchanged :)
UPDATE: Many other people say that
try/catch
are for error handling. They are not. They are for exception handling. In many languages, for example, trying to get a next element from the iterator on its last element will raise an exception; this is a perfectly valid use of exceptions. You can use them whenever something unexpected happens, which has to be handled outside the current scope (assuming you are not providing a callback to handle it).当然可以。但是,使错误处理变得更加容易所带来的好处是值得的。
Of course it does. But the gains from making error handling that much easier are worth it.