处理错误的最佳方式 [英] Best way to handle errors

查看:173
本文介绍了处理错误的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你们可以帮助我在这一个:如何处理错误和什么返回?
我做的大部分时间是使我的函数/方法返回两个可能的值。在成功的情况下的预期价值,如果发生故障,则为FALSE。这样我可以使用:

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

我不喜欢使用异常,因为一般来说,它们会打印某些东西并中断应用程序的运行流程。当然,我可以让他们返回一些东西,并显示一个警告与错误。但这太多了。而且我不喜欢宠物小精灵,试图抓住他们。 (Awsum topic,btw)



我关心的另一件事是编写一些错误处理驱动的代码。我们知道,用户几乎可以做任何事情导致意想不到的情况,并且代码预期这些错误太累了,坦白说,让我偏执。 XD



我为任何英文错误(我不写太频繁)道歉。
谢谢你阅读这个问题。 :D



PS:我读过有关防守编程和其他问题,但他们并没有完全回答我的疑问。

解决方案

也许你不会找到我的答案有用,因为我不是php开发人员,反正我会尝试给你常见的提示,因为错误处理辩论在任何编程语言中都是常见的。



1)首先尝试避免实际需要进行错误处理。这的确意味着尝试以尽可能少的错误条件的方式进行编码。一个简单的例子:通常,尝试清除空集合不应该引起错误。



2)不同于什么是支持的失败而不是不支持。支持的故障是您实际想要跟踪和处理作为程序逻辑的一部分的错误条件。不支持的失败将是错误状况,其实是代码错误。例如:您正在尝试在地图中查找元素,它是程序逻辑的一部分,特定元素应该在那里。没有任何意义来添加代码来测试元素是否存在,如果不存在则引发错误:您做出了假设。



这两点,我很确定你会明白为什么例外是如此有用的,以产生更清洁和更有效的代码。诀窍是:使您的代码尽可能少地提供异常(再次:不同的支持和不受支持的错误条件来选择实际需要什么异常),并且仅在最外部的范围捕获它们,您的应用程序用户与其进行交互和1)应该被警告,2)应该决定该做什么。在大多数情况下,编程性地抓住了一个缺点,这是一件坏事,因为它使得开发人员认为捕获的异常是有效的,就像有条件地做某事(if-then-else)当出现错误时不是案件。例外情况在您的应用程序抛出之前不会造成任何开销。


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.

解决方案

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.

这篇关于处理错误的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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