简体   繁体   English

应用程序级别的包罗万象的错误处理?

[英]Catch-all error handling on application level?

I have a WPF form with a couple of buttons, and for each button I have error handling code:我有一个带有几个按钮的 WPF 表单,对于每个按钮我都有错误处理代码:

try {bla bla} 
catch(Exception e){
  more bla
}

Is there any way I can set something up on the application level or something that will just catch all uncaught errors and display some generic message/ log the error?有什么方法可以在应用程序级别设置一些东西,或者只是捕捉所有未捕获的错误并显示一些通用消息/记录错误? Now I have to create handling for every button so the code doesn't crash.现在我必须为每个按钮创建处理,这样代码就不会崩溃。 It's an internal app so just displaying the message from whatever was thrown down there will suffice.这是一个内部应用程序,因此只需显示来自那里的任何内容的消息就足够了。 After that the app would just wait for the next button click, so it wouldn't have to do anything afterwards.之后,该应用程序将等待下一个按钮单击,因此之后无需执行任何操作。

There so much repetive code right now, wondering if there is some way to consolidate that and only deal with cases where there is a specific way to handle a specific error.现在有这么多重复的代码,想知道是否有某种方法可以巩固它,并且只处理有特定方法来处理特定错误的情况。

Regards Gert-Jan问候格特扬

Add this to you Global.asax :将此添加到您Global.asax

protected void Application_Error(object sender, EventArgs e) {
    Exception currentException = Server.GetLastError();

    // your handling code goes here ...

    Server.ClearError();
}

You can subscribe to AppDomain.UnhandledException event (see MSDN ) to accomplish this.您可以订阅AppDomain.UnhandledException事件(请参阅MSDN )来完成此操作。 Ideally, this happens when bootstrapping your application.理想情况下,这发生在引导您的应用程序时。 You can find the AppDomain you're executing in via AppDomain.Current .您可以通过 AppDomain.Current 找到您正在执行的AppDomain.Current

The answer depends on your platform.答案取决于您的平台。 In a web application you can bind the Application_Error event in Global Asax.在 web 应用程序中,您可以在 Global Asax 中绑定Application_Error事件。 In WCF you can inject an error handler of type System.ServiceModel.Dispatcher.IErrorHandler into the WCF stack, in forms applications you can bind the ThreadException event.在 WCF 中,您可以将System.ServiceModel.Dispatcher.IErrorHandler类型的错误处理程序注入 WCF 堆栈,在 forms 应用程序中,您可以绑定ThreadException事件。

Using it may imho be a good idea in some situations since you prevent showing exception details to the user, but also indicates a certain slobbyness regarding exception handling.在某些情况下使用它可能是一个好主意,因为您会阻止向用户显示异常详细信息,但也表明在异常处理方面存在一定的懒惰。 I have used WCF error handlers to convert domain exceptions to http status code which is simple.我已经使用 WCF 错误处理程序将域异常转换为简单的 http 状态代码。 Whether it is a good practice or not I do not know.我不知道这是否是一个好习惯。 For asp.net applications it is also worth looking at elmah available via NuGet .对于 asp.net 应用程序,还值得查看可通过elmah获得的NuGet

It is also possible to write a simple exception handler that allows you to repeat the try/catch blocks by sending reoutines as Func or Action like this也可以编写一个简单的异常处理程序,允许您通过像这样发送作为FuncAction的重新程序来重复 try/catch 块

    var eh = new ExceptionHandler();

    eh.Process ( () => throw new SillyException());

with class ExceptionHandler使用 class ExceptionHandler处理程序

    class ExceptionHandler 
    { 
        public T Process(Func<T> func()) 
        {
           try { return func(); }
           catch(Exception ex) { // Do stuff }
        }

        public void Process(Action a) 
        { 
            try { action() }
            catch(Exception ex) { // Do stuff }
        }
    }

If it is a WinForms app, you can encapsulate the Application.Run() call in a try catch statement, although I really wouldn't recommend this, you should only be using try catches where they are absoloutely needed and deal with exceptions whereever possible before just relying on catches.如果它是 WinForms 应用程序,您可以将 Application.Run() 调用封装在 try catch 语句中,尽管我真的不建议这样做,您应该只在绝对需要它们的地方使用 try catch 并尽可能处理异常在仅仅依靠捕获之前。

I know this method works though as I had to use it temporarily once to do some cleaning up on the crashing of an application.我知道这种方法有效,因为我不得不暂时使用它一次来清理应用程序的崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM