【问题标题】:Global Exception Handling for winforms controlwinforms控制的全局异常处理
【发布时间】:2010-09-05 09:22:15
【问题描述】:
在处理 ASP.NET 1.1 项目时,我总是使用 Global.asax 来捕获所有错误。我正在寻找一种类似的方法来捕获 Windows 窗体用户控件中的所有异常,该控件最终成为托管的 IE 控件。做这样的事情的正确方法是什么?
【问题讨论】:
标签:
winforms
error-handling
user-controls
【解决方案2】:
目前在我的 winforms 应用程序中,我有Application.ThreadException 的处理程序,如上所述,还有AppDomain.CurrentDomain.UnhandledException
大多数异常通过 ThreadException 处理程序到达,但根据我的经验,AppDomain 也遇到了一些
【讨论】:
-
来自 MSDN 的示例代码展示了如何捕获这两种类型的未处理异常:msdn
【解决方案5】:
来自 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