Global Exception Handler Not Catching Errors

Add-in Express™ Support Service
That's what is more important than anything else

Global Exception Handler Not Catching Errors
How to add a global exception handler in VB.NET within an ADX BHO? 
Gary Garside




Posts: 15
Joined: 2012-10-19
Hi all,

I'm hoping someone can help me. We've had our ADX IE BHO live in the wild for some time. Due to the massive complexity of it, we're getting exception stack traces submitted by users from time to time (which in itself is massively helpful!). These relate to anything from interfacing with the Registry, to casting mshtml elements incorrectly.

As a result, and to improve the user experience for our 30,000+ BHO users, we've been trying to add a global exception handler within the BHO to no avail. Here's our current code for adding the handlers (this takes place in the IEModule's OnConnect sub):

AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf IeModule_ExceptionHandler
AddHandler Application.ThreadException, AddressOf IeModule_ThreadHandler


And here's the corresponding sub's (also located in IEModule):

Private Sub IeModule_ExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
        Dim EX As Exception
        EX = e.ExceptionObject
        Console.Log("[UNHANDLED] " & EX.StackTrace)
    End Sub

    ' Catch a ThreadException event.
    Private Sub IeModule_ThreadHandler(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)
        Console.Log("[UNHANDLED] " & e.Exception.Message.ToString)
    End Sub


We also have a sub handling the ADX's "Me.OnError" event which essentially works the same as the above 2 handlers. None of these however are catching any exceptions thrown by the BHO. Popping the below code (which should throw an exception) in to a sub causes an unhandled exception dialog box within IE:

Dim X As Integer = 5
        X = X / 0


Can anyone spot anything we're doing wrong? Are we adding event handlers in the wrong place? Has anyone successfully achieved what we're trying to achieve before?

Thanks in advance,
Gary
Posted 31 Jul, 2014 05:32:23 Top
Gary Garside




Posts: 15
Joined: 2012-10-19
Excuse the excessive code tabbing above, that isn't present in the actual code. I should also mention that "Console.Log" is our own custom class/function set that we do a few things with - this is not a source of errors.
Posted 31 Jul, 2014 05:34:46 Top
Andrei Smolin


Add-in Express team


Posts: 18823
Joined: 2006-05-11
Hello Gary,

That event handler would be called if your add-on were a .NET application. Since the window procedure of IE doesn't dispatch any message to your add-on, this doesn't allow .NET to handle such a message and invoke the handlers above. Besides, an exception may occur outside of your add-on, AppDomain and .NET: an exception in the unmanaged code cannot be passed to managed code for handling.

Use the OnError event of the module to get an exception that occurs in the events of Add-in Express components placed onto the module and events of the module itself. If you place any non-Add-in-Express component (e.g. Timer) onto the module, you are responsible for handling exceptions that occur in the events of that component.


Andrei Smolin
Add-in Express Team Leader
Posted 31 Jul, 2014 06:18:16 Top
Gary Garside




Posts: 15
Joined: 2012-10-19
Hi Andrei,

Thanks for your response. That does explain why the event handlers were refusing to catch exceptions. So, to your knowledge, there's no way of adding a "Global Exception Handler" within an IE BHO (for ADX code or otherwise)?

Thanks,
Posted 31 Jul, 2014 07:00:14 Top
Andrei Smolin


Add-in Express team


Posts: 18823
Joined: 2006-05-11
Gary,

There's no such way.


Andrei Smolin
Add-in Express Team Leader
Posted 31 Jul, 2014 08:18:05 Top