将异常冒泡到堆栈顶部可以吗? [英] Is it ok to bubble up the exception to the top of the stack?

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

问题描述

是否可以让异常冒泡到堆栈的顶部,而不是在每个方法中捕获它?我们应该在任何情况下执行它吗? .. 这种方法有什么微妙的问题或副作用(例如,异常细节的丢失,堆栈跟踪或内部异常细节等)?

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:

我正在移动现有的WSE3 Web服务到WCF,所以y客户端是WSE3客户端。

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

我已经添加了一个行为,以便在WCF中发生 FaultException 将被传达给客户端服务。当 OperationContract 方法中有异常时,我在客户端收到异常消息,没有任何问题。但是,除了 OperationContract 之外的其他方法,我会以某种方式获得安全相关的问题。我不能确定确切的原因。

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.

但是,作为一个工作,我以为从 OperationContract 只允许异常冒泡到 OperationContract

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:


  • 处理它(即不推翻)

  • 添加一些重要的上下文信息

每个方法都包含一个 try-catch 块,我的字面意思是每个方法:

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.

如果异常必须传递一些进程间边界(例如在WCF服务中),那么在您的异常暴露于世界的点,您可能希望首先捕获,记录并以IPC边界的兼容格式重新启动异常,以便您具有所有故障的日志记录

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

在许多情况下,还有一种替代机制仅为此而设计--WCF具有 IErrorHandler 接口,可以注册以一致的方式捕获和记录所有未处理的异常,而不需要在每个暴露的方法中使用try-catch块。

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天全站免登陆