locked
Where to catch unhandled exceptions RRS feed

  • Question

  • I have a large vb6 app that I would like to migrate to vb.net gradually using Interop.  My vb6 exe is displaying forms and using classes defined in my .Net code (which is a dll).  How or where can I define a global exception handler that will log unhandled .Net exceptions?  I found this nice snippet but it only works for Winform projects:

    Namespace My
    Partial Friend Class MyApplication

    Private Sub MyApplication_UnhandledException(ByVal sender As
    Object, ByVal e As
    Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs)
    Handles Me.UnhandledException

    MessageBox.Show(Environment.StackTrace) ' I would log the exception here
    e.ExitApplication = True

    End Sub
    End Class
    End Namespace

    Is there a way to log exceptions that occur in the .Net dll without putting a try catch in each and every property/method in the class project?

    Joel
    Tuesday, June 2, 2009 10:19 PM

Answers

  • Hi Joel,

    You may also want to try the AppDomain.UnhandledException event.

    A piece of sample code here:

    Imports System
    Imports Microsoft.VisualBasic
    
    Public Module MyModule
        Private WithEvents curDomain As AppDomain = AppDomain.CurrentDomain
    
        Sub Main()
            ' The line below will raise an ArgumentNullException
            Environment.SetEnvironmentVariable(Nothing, Nothing)
        End Sub
    	
        Private Sub UnhandledException(ByVal sender As Object, e As UnhandledExceptionEventArgs) Handles curDomain.UnhandledException
            Console.WriteLine(e.ExceptionObject)
            Console.WriteLine(e.IsTerminating)
        End Sub
    End Module
    For a better understanding of this event and its behavior, please refer to the document of this event (link provided in the first line). I would recommend you read the Remark part which will help you to know some detailed explanation of the event.

    If you still have questions regarding this topic, please let me know.

    Thanks,

    Jie
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.

    The CodeFx Project
    My Blog (CHS)
    Wednesday, June 3, 2009 6:56 AM
    Moderator

All replies

  • Hi Joel,

    You may also want to try the AppDomain.UnhandledException event.

    A piece of sample code here:

    Imports System
    Imports Microsoft.VisualBasic
    
    Public Module MyModule
        Private WithEvents curDomain As AppDomain = AppDomain.CurrentDomain
    
        Sub Main()
            ' The line below will raise an ArgumentNullException
            Environment.SetEnvironmentVariable(Nothing, Nothing)
        End Sub
    	
        Private Sub UnhandledException(ByVal sender As Object, e As UnhandledExceptionEventArgs) Handles curDomain.UnhandledException
            Console.WriteLine(e.ExceptionObject)
            Console.WriteLine(e.IsTerminating)
        End Sub
    End Module
    For a better understanding of this event and its behavior, please refer to the document of this event (link provided in the first line). I would recommend you read the Remark part which will help you to know some detailed explanation of the event.

    If you still have questions regarding this topic, please let me know.

    Thanks,

    Jie
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Send us any feedback you have about the help from MSFT at fbmsdn@microsoft.com.

    The CodeFx Project
    My Blog (CHS)
    Wednesday, June 3, 2009 6:56 AM
    Moderator
  • Hi Wang,

    unfortunately this sniplet won't work, if calling your dll from VB6 (calling from Net Console Application works perfect)

    Do you know why or how to solve this issue?

    Best regards,

    AH214
    Thursday, March 11, 2010 2:43 PM