处理错误的最佳方法

发布于 2024-10-03 05:49:24 字数 457 浏览 0 评论 0原文

我希望你们能在这个问题上帮助我:如何处理错误以及返回什么? 大多数时候,我所做的就是让我的函数/方法返回两个可能的值。成功时的预期值,失败时的预期值。我可以这样使用:

if(function()) { ... } else { ... }

我不喜欢使用异常,因为通常它们会打印一些内容并中断应用程序的功能流程。当然,我可以让他们返回一些内容并显示错误警报。但这工作量太大了。而且我不太喜欢口袋妖怪,不想尝试把它们全部抓到。 (Awsum 主题,顺便说一句)

我关心的另一件事是编写一些“错误处理”驱动的代码。正如我们所知,用户几乎可以做任何事情来导致意外情况,而编写预期这些错误的代码太累人了,坦率地说,让我变得偏执。 XD

对于任何英语拼写错误,我深表歉意(我不经常写信)。 感谢您阅读这个问题。 :D

PS:我阅读了有关防御性编程和其他问题的内容,但它们并没有完全回答我的疑问。

I was hoping you guys could help me on this one: how to handle errors and what to return?
What I do, most of time, is make my functions/methods return two possible values. The intended value in case of success and FALSE in case of failure. This way I can use:

if(function()) { ... } else { ... }

I don't like to use exceptions because, generally, they print something and interrupt the functioning flow of the application. Of course, I can make them to return something and show an alert with the error. But it's too much work. And I don't like Pokemon so much to try to catch them all. (Awsum topic, btw)

Another thing I'm concerned about is to code something "error-handling" driven. As we know, users can do almost anything to cause an unexpected situation, and to code expecting these errors is too tiring and frankly, is making me paranoid. XD

I apologize for any english mispelling(I don't write too often).
Thank you for reading this question. :D

PS: I read about defensive programming and the other questions but they don't quite answer my doubt.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

音盲 2024-10-10 05:49:24

也许您不会发现我的答案有用,因为我不是 php 开发人员,无论如何,我会尝试为您提供常见提示,因为错误处理争论在任何编程语言中都很常见。

1)首先尽量避免实际需要的错误处理。这确实意味着尝试以尽可能少的错误情况进行编码。一个简单的例子:尝试清除空集合不会引发错误,这是常识。

2) 区分什么是受支持故障和不受支持故障。支持的故障是您实际上想要作为程序逻辑的一部分进行跟踪和处理的错误条件。不受支持的故障是一种实际上是代码错误的错误情况。例如:您正在尝试在地图中查找一个元素,并且该特定元素应该在那里是程序逻辑的一部分。添加代码来测试该元素是否存在并在不存在时引发错误是没有意义的:您假设它在那里。

在理解了这两点之后,我很确定您会理解为什么异常对于生成更清晰、更高效的代码如此有用。技巧是:让您的代码引发尽可能少的异常(同样:不同的支持和不支持的错误条件来选择实际需要的异常)并仅在应用程序的用户与之交互的最外部范围内捕获它们1)应该被警告,2)应该决定做什么。在大多数情况下,在没有用户交互的情况下以编程方式捕获异常是一件坏事,因为它使开发人员认为捕获异常是有效的,就像在错误条件出现但不是错误条件时有条件地执行某些操作(if-then-else)一样案件。异常在被抛出之前不会给您的应用程序带来任何开销。

Maybe you won't find my answer useful because I'm not a php developer, anyway I will try to give you common hints as error handling debates are common in any programming language.

1) First try to avoid the actual need for error handling. This does mean try to code in a way that error conditions are few as possible. A simple example: it's common sense that trying to clear an empty collection should not raise an error.

2) Distinct what is a supported failure than an unsupported one. A supported failure is an error condition that you actually want to track and handle as part of the program logic. An unsupported failure would be an error condition that's actually a code error. For example: you are trying to lookup an element in a map and it's part of the logic of your program that the specific element should be there. There's no sense to add code to test if the element was there and raise an error if it wasn't: you made the assumption it was there.

After you have understood these two points, I'm pretty sure you'll understand why exceptions are so useful to produce cleaner and more efficient code. The trick is: make your code to raise as few exceptions as possible (again: distinct supported and unsupported error conditions to choose what exceptions are actually needed) and catch them only at the most external scope, where the user of your application interact with it and 1)should be warned, 2)should decide what to do. Programmatically catch an excpetion without user interaction is, most of the times, a bad thing because it makes the dev think that catching exceptions is efficient just like conditionally do something (if-then-else) when an error condition arise when this is not the case. Exceptions won't cause any overhead to your application until they are thrown.

变身佩奇 2024-10-10 05:49:24

您是否尝试过使用 PHP 的错误日志工具? http://php.net/manual/en/book.errorfunc.php

然后,您可以使用 error_log() 之类的工具将自定义错误记录到您想要的任何日志文件中。

Have you tried working with PHP's error logging tools? http://php.net/manual/en/book.errorfunc.php

You can then use things like error_log() to shoot your custom errors to whatever log file you want.

痴者 2024-10-10 05:49:24

您应该考虑使用异常。如果您不愿意,他们不会打印任何内容。至少这可以更好地分离实际代码和错误处理。

你是对的,让函数返回一个值或在错误时返回 false 并不是最好的风格(是的,我知道这可能已经做了很多)。

如果您想了解有关良好编码实践的更多信息,我建议阅读 Robert Martin 的《干净的代码》

You should consider using Exceptions. They don't print anything if you don't want to. At least this would be a better separation of your actual code and error handling.

You are right, having a function return a value or return false on error is not the best style (yes I know that this is probably done a lot).

If you want to learn more about good coding practice, I suggest to read Clean Code by Robert Martin.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文