0

I am trying to figure out why this is happing. In my vb.net application I set a global handler in the ApplicationEvents.vb which I thought would only pick up any unhandled exceptions, although it is picking up every exception that happens in my application whether or not they are handled with try catch blocks. Here is my code in applicationevents

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

            e.ExitApplication = _
            MessageBox.Show(e.Exception.Message & _
                    vbCrLf & "The application has encountered a bug, would you like to                 Continue?", "An Error has occured.", _
                    MessageBoxButtons.YesNo, _
                    MessageBoxIcon.Question) _
                    = DialogResult.No
    End Sub

In the rest of my application I set normal try catch blocks like this:

     Try


          Catch ex as exception


     End Try

Can anyone tell me why this is happening?

Sami
  • 8,168
  • 9
  • 66
  • 99
user1632018
  • 2,485
  • 10
  • 52
  • 87
  • possible duplicate of [What's the difference between Application.ThreadException and AppDomain.CurrentDomain.UnhandledException?](http://stackoverflow.com/questions/2014562/whats-the-difference-between-application-threadexception-and-appdomain-currentd) – Hans Passant Nov 15 '12 at 17:08

1 Answers1

1

I created a VB project. The ApplicationEvents.vb looks like that:

Partial Friend Class MyApplication

    Private Sub MyApplication_UnhandledException(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
        MsgBox("Unhandled exception")
    End Sub
End Class

The code of the form is:

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim a As Integer
        Dim b As Integer

        Try
            a = a / b
        Catch ex As Exception
            MsgBox("Handled exception")
        End Try

    End Sub
End Class

Something important is that if you don't catch the error in the form, the general error handler might not be triggered (depending a setting on Visual Studio, I don't remember which one). To be sure, execute your project without debugging it (CTRL+F5 instead of F5)

Damir Arh
  • 17,637
  • 2
  • 45
  • 83
Serge Bollaerts
  • 324
  • 2
  • 6