C#(不是ASP / MVC /的​​WinForms) - 捕捉一类的所有异常 [英] C# (not ASP/MVC/WinForms) - Catch all exceptions in a class

查看:188
本文介绍了C#(不是ASP / MVC /的​​WinForms) - 捕捉一类的所有异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程在使用专有的编程语言,利用专门归因.NET类的私有code的选项的系统。

I am programming in a system that uses a proprietary programming language, with the option of using specially attributed .Net classes in the proprietary code.

不幸的是,该系统不处理未处理的异常,从净$ C $冒泡Ç好,如果实际上不是在所有;该系统没有解释崩溃。这是烦人的,因为我们经常要处理在的专有系统异常的,而不是净code。通过该系统的供应商所提供的解决方案是重新打包异常进入一个特殊的对象,该系统的确实的手柄。

Unfortunately, the system doesn't handle unhandled exceptions bubbling up from .Net code well, if fact not at all; the system crashes with no explanation. This is annoying, because we often want to handle exceptions in the proprietary system, not in .Net code. The solution offered by the vendor of the system is to repackage the exception into a special object that the system does handle.

我们的净code被写入在一个Facade模式,而问题是,以确保每一个从.NET code冒泡的异常处理,在外观的每个方法都必须包括一试/是重新包装可能发生的任何异常catch块。

Our .Net code is written in a façade pattern, and the problem is that to make sure every exception that bubbles up from the .Net code is handled, every method in the facade must include a try/catch block that repackages any exceptions that may occur.

我已经读了很多的线程这里描述相似的场景,其中大部分WinForms-或网络相关。因为我们的code是两者都不是,问题是,如果有一些办法赶上类中的所有的异常,使我们可以重新包装和重新抛出他们的修改版本?

I've read a lot of threads here describing similar scenarios, most of them WinForms- or web-related. Because our code is neither, the question is if there is some way to catch all exceptions in a class, so that we can repackage it and rethrow a modified version of them?

显然,NET的DLL的包含类和专有语言是完全超出了我们的控制。之间的接口

Obviously, the interface between the .Net dll's containing the classes and the proprietary language is completely beyond our control.

我试图通过@VMAtm提出的 currentDomain.UnhandledException 法,可惜没有效果。事件处理程序不火,而父系统拉住了异常,然后不守规矩照旧。这使我到谷歌再一次,我发现这一段<一个href="http://www.codinghorror.com/blog/2005/02/console-apps-and-appdomaincurrentdomainunhandledexception.html">here:

I tried the currentDomain.UnhandledException method suggested by @VMAtm, unfortunately to no avail. The event handler didn't fire, and the parent system got hold of the exception and then misbehaved as usual. That led me onto Google once more, and I found this paragraph here:

要了解的第一件事是,UnhandledException事件不是未处理的异常处理程序。注册事件,相反的是该文件说:-(,不会导致未处理的异常进行处理。(从那时起,他们就不会未处理的,但我会停止与循环论证已经...)的UnhandledException事件简单地通知您一个异常已经未处理的,如果你想尝试你的线程或应用程序死亡之前保存的状态。

The first thing to understand is that the UnhandledException event is not an unhandled exception "handler". Registering for the event, contrary to what the documentation says :-(, does not cause unhandled exceptions to be handled. (Since then they wouldn't be unhandled, but I'll stop with the circular reasoning already...) The UnhandledException event simply notifies you that an exception has gone unhandled, in case you want to try to save state before your thread or application dies.

乔纳森Keljo,CLR异常PM

Jonathan Keljo, CLR Exceptions PM

这太糟糕了,我喜欢有一个全球性的try / catch块的想法。我想它的意思是,我不是成功的从父系统隐藏例外。因为我不知道这是如何在系统中实现的第一件事(坦白地说,我不知道我怎么会去实现它自己的第一件事情),我就真的如履薄冰我假设,因此,如果任何人都可以纠正我以任何方式,请继续!

That was too bad, I liked the idea of having a "global" try/catch block. What I guess it means is that I'm not successful in hiding the exception from the parent system. Since I don't know the first thing about how this is implemented in that system (and frankly, I don't know the first thing about how I'd go on to implement it myself) I'm on really thin ice with my assumptions, so if anyone can correct me in any way, please go ahead!

哦,我收到父系统误差异常被抛出调用的目标。,这是因为据我所知的消息从外净异常发生。如果它是可以读出什么信息,我不知道。

Ohh, the error I'm getting in the parent system is Exception has been thrown by the target of an invocation., which is as far as I know the message from the outer .Net exception occurring. If it's possible to read anything out of that, I don't know.

我有一个去城堡动态代理由@jlew建议为好,但它看起来很多困难比二的AppDomain线吓死我了相当多的:)

I'll have a go at the Castle Dynamic Proxy suggested by @jlew as well, but it looked a lot harder than the two AppDomain lines and scared me quite a bit :)

如果您有我有同样的问题,你应该尝试通过@VMAtm建议先 currentDomain.UnhandledException 的方法,因为它是因为我的母系统是特别肛门没有奏效。

If you are having the same problem as I had, you should try the currentDomain.UnhandledException method suggested by @VMAtm first, because it's because of my parent system being especially anal it didn't work.

我把它用在城堡DynamicProxy设置工作。这是真的很容易成立。我的测试案例是一个门面类封装了XmlAttribute类。首先我要做的就是写的代理类:

I got it working by using the Castle DynamicProxy setup. It was really very easy to set up. My test case was a façade class encapsulating the XmlAttribute class. The first thing I had to do was to write the proxy class:

public class AttribInterceptor : IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
        try
        {
            invocation.Proceed();
        }
        catch (Exception e)
        {
            // Custom exception repackaging here
        }
    }
}

然后,我不得不指示外观对象实际使用的代理服务器。我一直在我的老后端领域,但增加了以下到c'tor:

Then I had to instruct the façade object to actually use the proxy. I kept my old backend field, but added the following to the c'tor:

public class CapXmlAttribute : CapPmlNetObject
{
    private XmlAttributeBackend _xmlAttribute;

    public CapXmlAttribute()
    {
        var generator = new ProxyGenerator();
        _xmlAttribute = (XmlAttributeBackend) generator.CreateClassProxy(
            typeof (XmlAttributeBackend), new AttribInterceptor());
    }
}

最后一步就是设置在后端的所有方法暴露于立面为虚拟。这对我来说没有问题,但可能是一个因素在于为别人着想。

The last step was setting all methods in the backend that is exposed to the façade as virtual. This was no problem for me, but might be a dealbreaker for others.

DynamicProxy 真的不是很好的记载,但我学到了很多的克日什托夫·Koźmic的教程汉密尔顿Verissimo的$ C $的CProject

DynamicProxy really isn't that good documented, but I learned a lot from Krzysztof Koźmic's tutorial and Hamilton Verissimo's codeproject.

推荐答案

我会看看使用类似的城堡动态代理的。照片 这将使你的类方法调用一个通用的方法,它会给你一个集中的地方放一个一揽子的异常处理程序被截获。 (这就是说,它是我不清楚你的类是如何实际实例,这可能使这种方法有问题)

I would take a look at using something like Castle Dynamic Proxy.
This will allow your class method calls to be intercepted in a generic way, which would give you a central place to put a "catch-all" exception handler. (That said, it's unclear to me how your classes are actually instantiated, which might make this approach problematic)

这篇关于C#(不是ASP / MVC /的​​WinForms) - 捕捉一类的所有异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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