大佬教程收集整理的这篇文章主要介绍了vb.net – 处理VB中的全局异常,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
Public Event UnhandledException As UnhandledExceptionEventHandler Private Sub form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles MyBase.Load Dim currentDomain As AppDomain = AppDomain.CurrentDomain AddHandler currentDomain.UnhandledException,AddressOf MyHandler End Sub Sub MyHandler(ByVal sender As Object,ByVal args As UnhandledExceptionEventArgs) Dim e As Exception = DirectCast(args.ExceptionObject,Exception) Using sw As New StreamWriter(File.open(myFilePath,FileMode.Append)) sw.WriteLine(Date.now & e.toString) End Using messageBox.Show("An unexcpected error occured. Application will be terminated.") Application.Exit() End Sub Private Sub button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles button1.Click Throw New Exception("Dummy Error") End Sub
我正在尝试全局捕获所有异常并在运行时创建日志文件,这在调试器中工作正常(异常处理和文本文件编写)但在我在安装项目中构建并安装到计算机后无法捕获任何未处理的异常.我错过了什么?我是否需要在设置项目中包含其他组件?非常感谢帮助
>转到项目 – >属性 – >单击应用程序并单击底部/附近的“查看应用程序事件”按钮.
>这将打开ApplicationEvents.vb.
>在左侧菜单中选择(MyApplicationEvents);和右边的UnhandledException.这将打开一个典型的事件处理程序,您可以在其中添加代码:
Private Sub MyApplication_UnhandledException(sender As Object,e As ApplicationserviceS.UnhandledExceptionEventArgs) Handles Me.UnhandledException Dim myFilePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),"badjuju.log") Using sw As New StreamWriter(File.open(myFilePath,FileMode.Append)) sw.WriteLine(datetiR_924_11845@e.Now) sw.WriteLine(e.Exception.messagE) End Using messageBox.Show("An unexcpected error occured. Application will be terminated.") End End Sub
在IDE运行时,这不会捕获异常,因为VS首先捕获它们,因此您可以看到它们并修复它们.
以上是大佬教程为你收集整理的vb.net – 处理VB中的全局异常全部内容,希望文章能够帮你解决vb.net – 处理VB中的全局异常所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。