错误处理我应该抛出异常吗?还是处理来源? [英] Error Handling Should I throw exception? Or handle at the source?

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

问题描述

我有这种格式

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

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

所以视图调用了具有业务/验证逻辑的服务层,它转而调用Repository。

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

现在我的服务层方法通常有一个bool返回类型所以如果数据库查询已经很好,我可以返回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.

像现在我的存储库有更新,创建,删除的无效返回类型。

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

所以说如果更新失败,如果我有一个try / catch在我的存储库中抛出错误,那么我的服务层捕获它,并执行elmah信令并返回false?

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?

或者我应该这些存储库方法返回一个bool,尝试/捕获存储库中的错误,然后将true或false返回给服务层,然后又向视图返回true还是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:


  • 在低级别时,由于异常而导致操作无法完成

  • 在中间层中,捕获多个异常类型并重新包装在一个异常类型中。

  • 在最后一个负责任的时刻处理例外。

  • 文件!

  • At low levels, throw when an operation cannot complete due to exceptional circumstances.
  • In middle layers, catch multiple exception types and rewrap in a single exception type.
  • Handle exceptions at the last responsible moment.
  • DOCUMENT!

以下是多层ASP.NET MVC应用程序(UI,Controller,Logic,Security,Repository)的伪代码示例:

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


  1. 用户单击提交按钮。

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

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


    • 用户无效


      • 安全层抛出SecurityException

      • 逻辑层捕获,在一个更通用的错误消息中包含在LogicException中

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

  1. User clicks submit button.
  2. Controller action is executed and calls into the Logic (business) layer.
  3. Logic method calls into Security with the current User credentials
    • User is invalid
      • Security layer throws SecurityException
      • Logic layer catches, wraps in LogicException with a more generic error message
      • Controller catches LogicException, redirects to Error page.

  • 存储库失败


    • 存储库抛出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.

    首先,可以访问堆栈跟踪。第二,调用者只需要处理一个异常类型,而不是多个异常。第三,技术异常消息可以按摩以显示给用户,同时仍然保留原始异常消息。最后,只有负责处理用户输入的代码可以真正知道用户的意图,并确定操作失败时的适当响应。存储库不知道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天全站免登陆