Exceptions in Visual Studio 2010 AddIn

53 views Asked by At

I have a WinForms application raising exceptions from code in an EventHandler (E.G. a Button click subscriber);

I have a try..catch block around the invocation of the ShowDialog of the Form;

The exception does not propagate to the try..catch block, but it just stops at the handler and the Form gets closed.

1) How can I propagate the exception to the block?

2) What is the best practice in these cases?

This is a new VS 2010 AddIn probject with a simple Tools menu entry added by the wizard:

    Public Sub Exec(ByVal commandName As String, ByVal executeOption As vsCommandExecOption, ByRef varIn As Object, ByRef varOut As Object, ByRef handled As Boolean) Implements IDTCommandTarget.Exec
        handled = False
        If executeOption = vsCommandExecOption.vsCommandExecOptionDoDefault Then
            If commandName = "MyAddin1.Connect.MyAddin1" Then

                Dim form1 = New Form1

                Try
                    form1.ShowDialog()
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try

                handled = True
                Exit Sub
            End If
        End If
    End Sub

The only content of the Form is a button with a static handler raising a "New Exception()".

When I click the button, the exception is not caught.

1

There are 1 answers

2
Carlos Quintero On

In Windows Forms each event handler needs its own try/catch block, or you can try to use Application.ThreadException although maybe it is not possible in an add-in. See Top-Level Exception Handling In Windows Forms Applications