处理VB中的全局异常 [英] Handle global exceptions in VB

查看:227
本文介绍了处理VB中的全局异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有这个项目遇到一些问题,应该是问题处理程序的代码。

 公共事件UnhandledException As UnhandledExceptionEventHandler 

Private Sub form1_Load(ByVal sender As System.Object,ByVal e作为System.EventArgs)处理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)

使用sw作为新的StreamWriter(File.Open(myFilePath, FileMode.Append))
sw.WriteLine(Date.now& e.toString)
结束使用

MessageBox.Show(发生未预期的错误,应用程序将被终止。)
Application.Exit()
End Sub

Private Sub button1_Click(ByVal sender As System.Object,ByVal e As Sy b.bb
$ b $ / code $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $

我试图在运行时全局捕获所有异常并创建日志文件,这在调试器(异常处理和文本写入)中工作正常,但是在我在安装项目中安装并安装后无法捕获任何未处理的异常进入机器我失踪了什么我需要在我的安装项目中添加其他组件吗?帮助将不胜感激

解决方案

已经有一种方法来处理整个应用程序的异常。将处理程序嵌入到表单中意味着只有在表单打开时才会被捕获并记录。


  1. 转到项目 - >属性 - >应用,然后点击底部附近的查看应用程序事件按钮。


  2. 这将打开 ApplicationEvents.vb


  3. 选择(MyApplicationEvents)在左侧菜单中和 UnhandledException 在右边。这将打开一个典型的事件处理程序,您可以添加代码:

      Private Sub MyApplication_UnhandledException(sender As Object,$ b $作为ApplicationServices.UnhandledExceptionEventArgs)处理Me.UnhandledException 

    Dim myFilePath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
    badjuju.log)

    使用sw作为新StreamWriter(File.Open(myFilePath,FileMode.Append))
    sw.WriteLine(DateTime.Now)
    sw.WriteLine(e.Exception.Message)
    结束使用

    MessageBox.Show(发生未预期的错误,应用程序将被终止)
    结束

    End Sub


当IDE运行时,这不会捕获异常,因为VS先抓所以你可以看到它们并修复它们。


Hello I have this project experiencing some problems with what is supposed to be my codes for the "problem" handler.

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

I'm trying to globally catch all exceptions and create logfile during runtime, which works fine in the debugger(exception handling and textfile writing) but cannot catch any unhandled exceptions after I build it in the setup project and Installed into a machine. What am I missing? Do I need to include additional components to my setup project? Help would be greatly appreciated

解决方案

There is already a way to handle exceptions for the entire application. Embedding the handler in a form means they would only be caught and logged if and while that form was open.

  1. Go to Project -> Properties -> Application and click the "View Application Events" button at/near the bottom.

  2. This will open ApplicationEvents.vb.

  3. Select (MyApplicationEvents) in the left menu; and UnhandledException in the right. This opens an otherwise typical event handler to which you can add code:

    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(DateTime.Now)
            sw.WriteLine(e.Exception.Message)
        End Using
    
        MessageBox.Show("An unexcpected error occured. Application will be terminated.")
        End
    
    End Sub
    

This will not catch exceptions while the IDE is running because VS catches them first so you can see them and fix them.

这篇关于处理VB中的全局异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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