在应用程序级别上捕获所有错误处理? [英] Catch-all error handling on application level?

查看:53
本文介绍了在应用程序级别上捕获所有错误处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个按钮的WPF表单,并且对于每个按钮,我都有错误处理代码:

I have a WPF form with a couple of buttons, and for each button I have error handling code:

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.

关于Gert-Jan

Regards Gert-Jan

推荐答案

有多种可能性:

  1. 建议可重复使用是否在C#中尝试/捕获块?
  2. 如果您有Winforms应用程序,请订阅事件 AppDomain.CurrentDomain.UnhandledException : Application.DispatcherUnhandledException AppDomain.CurrentDomain.UnhandledException

这篇关于在应用程序级别上捕获所有错误处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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