1

I am trying to write an unhandled exception event handler as in this question Catching application crash events

But the code as given won't compile, giving the message

error BC30590: Event 'UnhandledException' cannot be found.

How to fix? Do I need to import something (I'm new to VB) - if so then what?

Partial Friend Class MyApplication
    Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
        MsgBox(e.Exception.Message + vbNewLine + e.ToString())
    End Sub
End Class
Community
  • 1
  • 1
Sideshow Bob
  • 4,566
  • 5
  • 42
  • 79
  • 1
    Did you follow the steps where I said to enable the Application Framework and click the View Application Events button? – Steven Doggart Dec 05 '13 at 13:13
  • The `enable application framework` checkbox is checked, and I pasted the code from your answer into `ApplicationEvents.vb` which appeared when I clicked `view application events`. Is that right? – Sideshow Bob Dec 05 '13 at 13:24
  • Yes. If that's what you did, it should have worked. Can you please edit your question to show the entire contents of that file? – Steven Doggart Dec 05 '13 at 13:27
  • Done. I presume MyApplication is ok - I should't change that (my application as I see it consists of just a single class called Form1) – Sideshow Bob Dec 05 '13 at 13:30
  • That's the entire file? That isn't wrapped in a `Namespace My`/`End Namespace` block? – Steven Doggart Dec 05 '13 at 13:34
  • Yes. No namespace block. The app functionality sits in form1.vb which contains event handlers for a form. – Sideshow Bob Dec 05 '13 at 13:44

1 Answers1

2

In order for the MyApplication partial class to work, it must be in the same namespace as the primary MyApplication class. If it's not, that means that you are just creating a whole new MyApplication class which doesn't include that event. To fix your code, make sure that the partial class is in the My namespace, like this:

Namespace My
    Partial Friend Class MyApplication
        Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            ' ...
        End Sub
    End Class
End Namespace
Steven Doggart
  • 43,358
  • 8
  • 68
  • 105