0

I have a program with some DLL-extensions, that are loaded at runtime. The frontend is a CefSharp in a WinForms. But the program is already that big, uses some other 3rd party code parts (where I'm not able to take care of) and this program has to run without permanent maintenance at a far away costumer shop, that i need a global exception handler for uncaught exceptions, to handle these exceptions and log them, for analysis.

I already really searched for solutions and tutorials but there was nothing that worked for me.

The mainform just starts another assembly (and give him the form object) and this assembly "does it's thing". e.g. add the CefSharp-Browser to the form.

I tried to add a method to handle Handles Me.UnhandledException, MyBase.UnhandledException, MyClass.UnhandledException in the ApplicationEvents.vb

Namespace My     

    Partial Friend Class MyApplication

        Protected Overrides Function OnInitialize(ByVal commandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean

            Me.MinimumSplashScreenDisplayTime = 0

            AddHandler My.Application.UnhandledException, AddressOf UnhandledExceptionHandler

            Return MyBase.OnInitialize(commandLineArgs)

        End Function


        Public Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException, MyBase.UnhandledException, MyClass.UnhandledException

            Dim errstr = $"Message: {vbNewLine}
                      {e.Exception.Message}{vbNewLine}
                      {vbNewLine}
                      InnerMessage: {vbNewLine}
                      {e.Exception.InnerException.Message}{vbNewLine}
                      {vbNewLine}
                      Stacktrace: {vbNewLine}
                      {e.Exception.StackTrace.ToString()}{vbNewLine}"
            MsgBox($"Aa uncaught exception was thrwon! {vbNewLine} {vbNewLine} {errstr}")

        End Sub

    End Class

End Namespace

But this method UnhandledExceptionHandler is never called, even exceptions where thrown that are tagged as uncaught in the debugger and causes the debugging to pauses.

So if my question was not clear: Can anybody help me or can explain to me, where i made a mistake, where could I possibly made a wrong assumption or how such a uncaught exception handler would work.

in vb.net at Visual Studio 2017

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Horitsu
  • 272
  • 3
  • 12
  • Run the application directly, i.e. not in the debugger, and see what happens when an exception is thrown and not caught elsewhere. – jmcilhinney Jan 05 '18 at 07:21
  • The program does not crash but also it does not give me an information about an uncaught exception (and yes there is one, I took care of that) – Horitsu Jan 05 '18 at 07:44
  • Where exactly is the exception being thrown? If it's in the `Load` event handler of a form then that's the reason you're not seeing it. Such exceptions get swallowed on a 64-bit system. If you want to learn why, you can search for that. – jmcilhinney Jan 05 '18 at 08:03
  • The point is about that exception can be caused everywhere in the loaded extensions. When i know where the exception is thrown, i take care of it at this place to achieve a proper exception handling. But the question is about unknown exceptions, e.g. exceptions that are thrown by 3rd party code or by a corrupted settings configuration, etc pp. – Horitsu Jan 05 '18 at 08:30
  • But I saw something: The exception is caught by the debugger. The called function is called by the CefSharp browser via Javascript. But after continuing, a error is listet in the devTools-window of the CefSharp browser...after the debugger already catched the exception. This doesn't seems logical for me, that the debugger pauses the program while the exception seems to be catched be the Cef. – Horitsu Jan 05 '18 at 08:35
  • Interestingly, I just tested an exception in the `Load` event handler of a form and it worked "properly" too, so maybe Microsoft have finally addressed that glaring issue. So, I can see that it works as it should if done as I described. If you're talking about a exception generated in JavaScript code by a browser then of course that won't invoke your `UnhandledException` event handler because there's no managed exception. Unless the browser throws an exception in managed code, there's nothing for your app to catch. – jmcilhinney Jan 05 '18 at 08:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/162575/discussion-between-horitsu-and-jmcilhinney). – Horitsu Jan 05 '18 at 08:52
  • I decline the invitation. – jmcilhinney Jan 05 '18 at 08:54
  • The example exception is caused in my vb.net code, but the function is called by a javascript function in the CefSharp browser. And this javascript function doesn't have any exception handling. (And it also would not be the solution i search for to just add one there.) I will try to cause another exception that is not (more or less called by the CefSharp browser) – Horitsu Jan 05 '18 at 08:57
  • I caused an exception by purpose in an own thread (to get out of the normal calling chain) and without the debugger, the program now just crashes. The method to catch the uncaught exception was not called. So I'm still at the initial problem of my question. I have the hunch that I'm have a basic mistake with my thinking, but I'm also clueless what I have to searching for. I though: With a method that handles uncaught exceptions in the starting assembly, that calls the main extension, which loads and starts all the other ones, I can catch all these unknown exceptions from all the rest. – Horitsu Jan 05 '18 at 09:48
  • Like I said, it worked for me using the steps I specified in my answer below. I suggest that you follow those steps and see if they work for you. If they don't then something is wrong with your environment. If they do work then something is wrong with your project. That `Handles` clause and the `AddHandler` statement suggest that you have done things that there's no good reason to do. – jmcilhinney Jan 05 '18 at 10:32

1 Answers1

0

Here's what I just did to prove that it works as it should:

  1. Create a new WinForms project.
  2. Add a Button to the form and handle its Click event.
  3. Add code to event handler that will throw an exception, e.g. IO.File.OpenRead("this file does not exist").
  4. Open the Application page of the project properties and click the 'View Application Events' button.
  5. Select '(MyApplication Events)' in the middle drop-down at the top of the code window and then select UnhandledException in the right-most drop-down.
  6. Add some notification code to the event handler, e.g. MessageBox.Show(e.Exception.ToString()).
  7. Press F5 to run the project in the debugger and click the Button. Note that the debugger will notify you of the exception.
  8. Press Ctrl+F5 to run the app without debugging and click the Button. Note that your notification code is executed.
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46