它是确定冒泡异常堆栈的顶部? [英] Is it ok to bubble up the exception to the top of the stack?

查看:118
本文介绍了它是确定冒泡异常堆栈的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是确定让追赶它在每一个方法的异常泡到堆栈的顶部呢?......我们应该做的,在任何情况下? .. 是否有任何细微的问题或副作用,这种方法(如损失的异常堆栈跟踪或内部异常的详细信息等详细信息)?


虽然我的问题是,一般情况下,该方案在我的情况在present如下:

我将现有的WSE3 Web服务的WCF等Ÿ客户有WSE3客户。

我添加了一个行为,使的FaultException 将通报给客户端时,它发生在WCF服务。当在 OperationContract的方法的异常,我得到异常信息在客户端没有任何问题。但每当它出现在比 OperationContract的 S以外的方法,我莫名其妙地得到安全相关的问题。我不能够确定的确切原因。

不过,由于周围的工作我还以为只有扔从 OperationContract的例外,让到 OperationContract的。

解决方案
  

它是确定让追赶它在每一个方法的异常泡了呢?

不要抓住每方法excpetions! - 你应该的能赶上例外,如果你可以用它做什么有用的,例如:

  • 在处理它(即不重新抛出它)
  • 添加了一些重要的背景信息

我一直维护的应用程序,每一个nethod被包围了的try-catch 块,我从字面上的意思的每次的方法:

 公共无效DoSomething的()
{
    尝试
    {
        抛出新的NotImplementedException();
    }
    赶上(例外前)
    {
        扔ExceptionHandler.CreateException(例如,DoSomething的);
    }
}
 

这样的捕获异常是完全没有意义的,什么都不做,除了让你的code难以阅读和你的异常难以跟踪。

在这里您除了有通过一些进程间的边界(如在WCF服务),然后在您的例外暴露你可能想先抓了世界的角度,登录,然后在重新抛出异常的情况下,对于IPC边界兼容的格式,让你有一个日志中的所有失败在你servivce

在许多情况下,然而,有专为眼前这个目的而WCF有<一个替代机制href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler.aspx">IErrorHandler接口,可以注册捕获和记录以一致的方式所有未处理的异常,而不在每个公开的方法需要一个try-catch块。

Is it ok to let exception bubble up to the top of the stack instead of catching it in every method?.. Should we do it in any case? ..Are there any subtle issues or side effects with this approach (e.g. loss of details of the exception, stack trace or inner exception details etc.) ?


Though my question is general, the scenario in my case at present is as follows:

I am moving existing WSE3 web service to the WCF and so y clients are WSE3 clients.

I have added a behavior so that the FaultException will be communicated to the client side whenever it occurs in the WCF service. When there is an exception in the OperationContract method, I get exception message at client side without any problem. But whenever it occurs in the methods other than the OperationContracts, I get security related problem somehow. I am not able to identify the exact cause.

However, as a work around I thought to throw exceptions from OperationContract only and let exceptions bubble up to the OperationContract.

解决方案

Is it ok to let exception bubble up instead of catching it in every method?

Please Don't catch excpetions in every method! - you should only ever catch exceptions if you can do something useful with it, for example:

  • Handle it (i.e. not rethrow it)
  • Add some important contextual information

I've maintained applications where every nethod was surround with a try-catch block, and I literally mean every method:

public void DoSomething()
{
    try
    {
        throw new NotImplementedException();
    }
    catch (Exception ex)
    {
        throw ExceptionHandler.CreateException(ex, "DoSomething");
    }
}

Catching exceptions like this is utterly pointless and does nothing except make your code harder to read and your exceptions harder to trace.

In the case where you exception has to pass some interprocess boundary (such as in a WCF service) then at the point where your exception is exposed to the world you might want to first catch, log and then rethrow the exception in a compatible format for the IPC boundary so that you have a log of all failures in your servivce

In many cases however there is an alternative mechanism designed for just this purpose- WCF has the IErrorHandler interface which can be registered to catch and log all unhandled exceptions in a consistent way without needing a try-catch block in each exposed method.

这篇关于它是确定冒泡异常堆栈的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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