winforms winforms控件的全局异常处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Global Exception Handling for winforms control
提问by Alex
When working on ASP.NET 1.1 projects I always used the Global.asax to catch all errors. I'm looking for a similar way to catch all exceptions in a Windows Forms user control, which ends up being a hosted IE control. What is the proper way to go about doing something like this?
在处理 ASP.NET 1.1 项目时,我总是使用 Global.asax 来捕获所有错误。我正在寻找一种类似的方法来捕获 Windows 窗体用户控件中的所有异常,该控件最终成为托管的 IE 控件。做这样的事情的正确方法是什么?
采纳答案by Chris Leon
You need to handle the System.Windows.Forms.Application.ThreadExceptionevent for Windows Forms. This article really helped me: http://bytes.com/forum/thread236199.html.
您需要处理System.Windows.Forms.Application.ThreadExceptionWindows 窗体的事件。这篇文章对我很有帮助:http: //bytes.com/forum/thread236199.html。
回答by Orion Edwards
Currently in my winforms app I have handlers for Application.ThreadException, as above, but also AppDomain.CurrentDomain.UnhandledException
目前在我的 winforms 应用程序中Application.ThreadException,我有处理程序,如上所述,但也有AppDomain.CurrentDomain.UnhandledException
Most exceptions arrive via the ThreadExceptionhandler, but the AppDomain one has also caught a few in my experience
大多数异常是通过ThreadException处理程序到达的,但根据我的经验,AppDomain 也捕获了一些异常
回答by Brad Tutterow
If you're using VB.NET, you can tap into the very convenient ApplicationEvents.vb. This file comes for free with a VB.NET WinForms project and contains a method for handling unhandled exceptions.
如果您使用的是 VB.NET,您可以使用非常方便的 ApplicationEvents.vb。该文件随 VB.NET WinForms 项目免费提供,并包含处理未处理异常的方法。
To get to this nifty file, it's "Project Properties >> Application >> Application Events"
要获得这个漂亮的文件,它是“项目属性 >> 应用程序 >> 应用程序事件”
If you're not using VB.NET, then yeah, it's handling Application.ThreadException.
如果您没有使用 VB.NET,那么是的,它正在处理Application.ThreadException。
回答by Ajay
To Handle Exceptions Globally...
全局处理异常...
Windows Application
视窗应用
System.Windows.Forms.Application.ThreadException event
System.Windows.Forms.Application.ThreadException 事件
Generally Used in Main Method. Refer MSDN Thread Exception
一般用于 Main 方法。参考MSDN 线程异常
Asp.Net
ASP.NET
System.Web.HttpApplication.Error event
System.Web.HttpApplication.Error 事件
Normally Used in Global.asax file. Refer MSDN Global.asax Global Handlers
通常用于 Global.asax 文件。请参阅MSDN Global.asax 全局处理程序
Console Application
控制台应用程序
System.AppDomain.UnhandledException event
System.AppDomain.UnhandledException 事件
Generally used in Main Method. Refer MSDN UnhandledException
一般用于Main方法。参考MSDN UnhandledException
回答by Aaron Hoffman
Code from MSDN: http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
来自 MSDN 的代码:http: //msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx?cs-save-lang=1& cs-lang= vb#code- snippet-2
Sub Main()
Dim currentDomain As AppDomain = AppDomain.CurrentDomain
AddHandler currentDomain.UnhandledException, AddressOf MyHandler
Try
Throw New Exception("1")
Catch e As Exception
Console.WriteLine("Catch clause caught : " + e.Message)
Console.WriteLine()
End Try
Throw New Exception("2")
End Sub
Sub MyHandler(sender As Object, args As UnhandledExceptionEventArgs)
Dim e As Exception = DirectCast(args.ExceptionObject, Exception)
Console.WriteLine("MyHandler caught : " + e.Message)
Console.WriteLine("Runtime terminating: {0}", args.IsTerminating)
End Sub

