检查错误和抛出异常有多远? [英] How far to check for errors and throw exceptions?

查看:130
本文介绍了检查错误和抛出异常有多远?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的网站的后端工作,我想知道我应该多远去测试错误并相应地抛出异常?在方法和功能不当使用的意义上进行测试。不正确使用错误。



我最担心的是代码膨胀,并且使用一堆if / elses,类型检查和其他类似的测试你应该去考虑一切可能的方式来使用一个对象,捕获所有的不正当的用途,并抛出异常,让开发者使用它清楚。他们做错了什么?



或者是让代码自然而然的更好的方法(显然,捕捉和检查将是一个大的东西交易,但不是针对每一个偶然事件),并且依赖于使用代码阅读可用文档的用户,并相应地使用它。



有没有一般的最佳处理此类问题时遵循的做法或准则?我正在PHP工作,有什么具体的PHP,虽然这个问题一般是广泛的?



感谢任何帮助的事情。

解决方案

至少应用程序不应该中断:当检测到错误(是因为您检测到问题可能发生,或者是因为发生了问题),您应该显示一些不错的错误信息,最后记录错误的技术信息。



关于膨胀的代码:我不会把太多的测试和所有这些在我的代码中:它会最终导致难以理解和维护的东西 - 这很重要!





我一般尝试做的是:




  • 测试错误用户提供的数据


    • 例如,使用特定的类,所以这些检查不在处理数据库和业务规则的代码的中间。 / li>
    • 这些测试是相对精确的,以便为用户生成有用的错误消息


      • 例如:您不应该输入更多超过20个字符

      • 已经有该电子邮件地址的用户


        • 基本上这里的重点是用户。


      • 当用户提供数据似乎OK,我一起工作。


        • 如果发生某些错误,很有可能是技术错误

        • 应该记录哪个

        • 只有哎呀,发生错误应该显示给用户。

        • 在这里,测试不是很精确:我们只需要知道它是否工作 - 不一定是很好的细节。




      当然,最后,您应该确保数据库中的数据是正确的,并且不保存只有一半的数据。





      处理技术错误的常见方法是使用异常;这是一个非常基本的想法:

        try {
      //开始事务到DB

      //可能会失败并抛出异常的一些代码

      //某些其他代码可能会失败并抛出异常

      //这里的代码将不会被执行抛出异常

      //提交DB事务
      } catch(异常$ e){
      //回滚事务(取消发送到DB的查询)
      //将技术信息记录到文件
      //显示一个不错的消息
      }

      伟大的事情是,它允许将所有错误处理代码放在一个地方,并将较少的测试代码放在重要的东西之中;即让它失败,我们稍后会处理这些问题


      I'm working on the back-end of my website and I'm wondering how far I should be going to test for errors and throwing exceptions accordingly? Testing in the sense of improper usage of methods and functions. Not errors under correct usage.

      My biggest concern is code bloat and uglying it up with a bunch of if/elses, type checks, and other such tests.

      Should you go so far as to consider every possible way someone could use an object, catch all the improper uses, and throw exceptions to make it clear to the developer using it what they've done wrong?

      Or is it a better approach to let the code break naturally (obviously catching and checking for things that would be a big deal, but not for every contingency), and depend on anyone using the code to read the available documentation and use it accordingly?

      Are there any general best practices or guidelines to follow when handling such issues? I'm working in PHP, is there anything specific to PHP, although this issue is generally language wide?

      Thanks for any help on the matter.

      解决方案

      At least, your application should not "break" : when an error is detected (be it because you detected a problem could happen and avoided it, or because a problem did happen), you should display some nice error message, and, eventually, log the technical informations of the error.

      About bloating the code : I would not put too much tests and all that in my code : it would end up in something hard to understand and maintain -- which is important !


      What I generally try to do is :

      • Test for errors in the user-supplied data
        • Using a specific class, for instance, so those checks are not in the middle of the code that deals with database and business rules.
        • Those tests are relatively precise, in order to generate useful error messages for the user
          • For instance : "You should not input more than 20 characters"
          • Or "there is already a user with that e-mail address"
        • Basically, the important thing here is the user.
      • When user-supplied data seems OK, I work with it.
        • And there, if some error happens, it'll most likely be a technical error
        • Which should be logged
        • And only a "oops, an error occured" should be displayed to the user.
        • Which means that, here, tests are not as precise : we only need to know if it works or not -- not necessarily in great details.

      Of course, in the end, you should ensure that the data in the DB is correct, and that you don't save only half of the data.


      A common way of dealing with technical errors is using exceptions ; here is a very basic idea :

      try {
          // Begin transaction to the DB
      
          // Some code that might fail and throw an Exception
      
          // Some other code that might fail and throw an Exception
      
          // Code here will not be executed if an Exception has been thrown
      
          // Commit DB transaction
      } catch (Exception $e) {
          // Rollback transaction (cancels the queries that were sent to the DB)
          // Log technical informations to a file
          // Display a nice message
      }
      

      The great thing is that it allows one to place all error-handling code in a single place, and put less testing code in the middle of the important stuff ; i.e. "let it fail, we'll deal with the problems later"

      这篇关于检查错误和抛出异常有多远?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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