错误处理我应该抛出异常?或在源头处理? [英] Error Handling Should I throw exception? Or handle at the source?

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

问题描述

我有这种格式的

asp.net MVC视图 - >服务层 - >库

asp.net MVC View -> Service Layer -> Repository.

所以视图调用它有业务/验证逻辑在其中它轮流调用库。服务层

So the view calls the service layer which has business/validation logic in it which in turns calls the Repository.

现在我的服务层的方法通常有一个布尔返回类型,因此,如果数据库查询通过良好的走了,我可以返回true。或者如果它失败了。然后一个通用的消息被显示给用户。

Now my service layer method usually has a bool return type so that I can return true if the database query has gone through good. Or if it failed. Then a generic message is shown to the user.

我当然会记录错误与ELMAH。但是我不知道我应该怎么得到这一点。

I of course will log the error with elmah. However I am not sure how I should get to this point.

就像现在我的仓库有void返回类型的更新,创建,删除。

Like right now my Repository has void return types for update,create,delete.

所以说,如果更新失败,​​我应该有我的存储库中的try / catch抛出错误,那么我的服务层捕获并做ELMAH信令和返回假的?

So say if an update fails should I have a try/catch in my repository that throws the error, Then my service layer catches it and does elmah signaling and returns false?

或者我应该有这些库方法返回一个布尔,尝试/捕获错误在仓库中,然后返回真或假的服务层哪些又返回真或假来有何看法?

Or should I have these repository methods return a "bool", try/catch the error in the repository and then return "true" or "false" to the service layer what in turn returns "true" or "false" to the view?

异常处理还混淆了我如何处理错误,并在抛出时捕获错误。

Exception handling still confuses me how handle the errors and when to throw and when to catch the error.

推荐答案

根据经验我一直用的是:

The rule of thumb I always use is:


  • 在较低水平,当一个操作无法完成抛因特殊情况。

  • 在中层,赶多种异常类型,并在一个单一的异常类型重新包装。

  • 在最后责任时刻处理异常。

  • 文件!

下面是一个多层ASP.NET MVC应用程序(UI,控制器,逻辑,安全,存储库)的伪code的例子:

Here's an example in pseudocode for a multi-layer ASP.NET MVC app (UI, Controller, Logic, Security, Repository):


  1. 用户点击提交按钮。

  2. 控制器操作执行并调用逻辑(业务)层。

  3. 逻辑方法调用与当前用户凭据安全

    • 用户无效

      • 安全层抛出SecurityException异常

      • 逻辑层捕捞,包装在LogicException一个更通用的错误信息

      • 控制器捕捉LogicException,重定向到错误页面。


  • 库失败

    • 库抛出RepositoryException

    • 逻辑层捕捞,包装在LogicException一个更通用的错误信息

    • 控制器捕捉LogicException,重定向到错误页面。

    注意,逻辑层只是抛出一个异常类型 - LogicException。任何下级例外冒泡被发现,包裹在LogicException,它抛出一个新的实例。这给了我们很多优点。

    Notice, the Logic layer only throws a single exception type -- LogicException. Any lower-level exceptions that bubble up are caught, wrapped in a new instance of LogicException, which is thrown. This gives us many advantages.

    首先,堆栈跟踪访问。其次,主叫只需要处理一个单一的异常类型,而不是多个异常。三,技术异常消息可以按摩,以显示给用户,同时仍保留原来的异常消息。最后,只有code负责处理用户输入才能真正知道用户的意图,并确定适当的响应是什么时,操作失败。该库不知道是否应该UI显示错误页面或要求​​用户具有不同值再试一次。控制器知道这一点。

    First, the stack trace is accessible. Second, callers only have to deal with a single exception type rather than multiple exceptions. Third, technical exception messages can be massaged for display to users while still retaining the original exception messages. Lastly, only the code responsible for handling user input can truly know what the user's intent was and determine what an appropriate response is when an operation fails. The Repository doesn't know if the UI should display the error page or request the user try again with different values. The controller knows this.


    顺便说一句,没有人说你不能做到这一点:

    By the way, nothing says you can't do this:

    try
    {
      var result = DoSomethingOhMyWhatIsTheReturnType();
    }
    catch(LogicException e)
    {
      if(e.InnerException is SqlException)
      {
        // handle sql exceptions
      }else if(e.InnerException is InvalidCastException)
      {
        // handle cast exceptions
      }
      // blah blah blah
    }
    

    这篇关于错误处理我应该抛出异常?或在源头处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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