使用一个HttpModule异常处理 [英] Exception handling using an HttpModule

查看:245
本文介绍了使用一个HttpModule异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在审查该公司的系统的异常处理一个,发现一对夫妇有趣的事情。

We're reviewing one of the company's system's exception handling and found a couple of interesting things.

大部分code块(如果不是全部)是一个try / catch块里面,catch块新BaseApplicationException被抛出内 - 这似乎从企业库到来。
我在这里有点麻烦,因为我没有看到过利益这样做。 (抛出另一个异常随时之一发生)
一个谁在使用该系统已有一段时间的开发商说,这是因为该类的负责出版异常(发送电子邮件之类的东西),但他也不太清楚这件事。
花一些时间通过code,我很自信地说去后,这一切确实是收集有关环境和比其发布的信息。

Most of the code blocks (if not all of them) are inside a try/catch block, and inside the catch block a new BaseApplicationException is being thrown - which seems to be coming from the Enterprise Libraries. I'm in a bit of a trouble here as I don't see the benefits off doing this. (throwing another exception anytime one occurs) One of the developers who's been using the system for a while said it's because that class's in charge of publishing the exception (sending emails and stuff like that) but he wasn't too sure about it. After spending some time going through the code I'm quite confident to say, that's all it does is collecting information about the environment and than publishing it.

我的问题是:
  - 是否合理包装内所有尝试{}赶上{}块,不是抛出一个新的异常code?如果是,为什么?有什么好处?

My question is: - Is it reasonable to wrap all the code inside try { } catch { } blocks and than throw a new exception? And if it is, why? What's the benefit?

我个人的看法是,这将是更容易使用一个HttpModule,注册应用程序事件的错误事件,并做有什么模块内部必要的。如果我们愿意走这条路,我们会错过什么?什么缺点?

My personal opinion is that it would be much easier to use an HttpModule, sign up for the Error event of the Application event, and do what's necessary inside the module. If we'd go down this road, would we miss something? Any drawbacks?

您的意见备受AP preciated。

Your opinion's much appreciated.

推荐答案

从不 1 赶上(异常前)。期 2 。有没有办法可以处理所有不同类型的错误,你可能赶上。

Never1 catch (Exception ex). Period2. There is no way you can handle all the different kinds of errors that you may catch.

从不 3 捕捉异常派生的类型,如果你不能处理它,或者提供额外的信息(会被后续的异常处理程序使用)。显示错误消息的不可以一样的处理的错误。

Never3 catch an Exception-derived type if you can't handle it or provide additional information (to be used by subsequent exception handlers). Displaying an error message is not the same as handling the error.

一对夫妇的原因,从我的头顶:

A couple of reasons for this, from the top of my head:


  • 捕获并重新抛出昂贵

  • 您会最终失去堆栈跟踪

  • 您必须在你的code
  • 低信号对杂讯比
  • Catching and rethrowing is expensive
  • You'll end up losing the stack trace
  • You'll have a low signal-to-noice ratio in your code

如果你知道如何处理的具体异常(并重新申请pre-错误状态),捕获它。 (这就是为什么它被称为的例外处理

If you know how to handle a specific exception (and reset the application to pre-error state), catch it. (That's why it's called exception handling.)

要处理未捕获的异常,听取了相应的事件。在做的WinForms,你需要听 System.AppDomain.CurrentDomain.UnhandledException ,以及 - 如果你做的线程 - System.Windows.Forms.Application.ThreadException 对于Web应用程序,也有类似的机制( System.Web.HttpApplication.Error )。

To handle exceptions that are not caught, listen for the appropriate events. When doing WinForms, you'll need to listen for System.AppDomain.CurrentDomain.UnhandledException, and - if your doing Threading - System.Windows.Forms.Application.ThreadException. For web apps, there are similar mechanisms (System.Web.HttpApplication.Error).

至于在应用程序包装框架例外(非)特殊情况例外(即抛出新MyBaseException(除息); ):毫无意义,以及难闻的气味<。 SUP> 4

As for wrapping framework exceptions in your application (non-)specific exceptions (i.e. throw new MyBaseException(ex);): Utterly pointless, and a bad smell.4

修改

Edit

1 从不的是一个非常难听的话,尤其是当它涉及到工程,如@克里斯在评论中指出。我承认自己是高的原则,当我第一次写这个答案。

1 Never is a very harsh word, especially when it comes to engineering, as @Chris pointed out in the comments. I'll admit to being high on principles when I first wrote this answer.

2,3 1

4 如果你没有带来什么新表,我还是被这一立场。如果您已经引起异常前为您知道在任意数量的方式可能会失败的方法的一部分,我认为目前的方法应该反映在它的签名。正如你知道的,异常是不是方法签名的一部分。

4 If you don't bring anything new to the table, I still stand by this. If you have caught Exception ex as part of a method that you know could fail in any number of ways, I believe that the current method should reflect that in it's signature. And as you know, exceptions is not part of the method signature.

这篇关于使用一个HttpModule异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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