How do I trap errors in the published version of my software?

Robert Barnes 101 Reputation points
2021-08-16T22:40:05.313+00:00

MANASYS Jazz is a Windows Forms application with this code structure
Private Sub Startup_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Try ... Dim Workbench As New JazzWorkbench JazzWorkbench = Workbench Workbench.AutoOpen = My.Settings.AutoOpen Workbench.ShowDialog() CloseJazzLog() 'Does nothing unless open Me.Close() Catch ex As Exception lblError.Text = ex.Message WriteJazzLog("Error:" & ex.Message) ... CloseJazzLog() End Try End Sub
All the real logic is in JazzWorkbench, the point of Startup is that it runs JazzWorkbench within a Try/Catch so that if there is an error it is caught and reported. There is a Startup Form that is displayed when an error is trapped. I'd just changed this form to add a button to make it easy for users to report errors to us.

This all works perfectly when I run the development version from Visual Studio. Here I've started MANASYS Jazz and clicked a [Test] button that forces an error. The Startup form is displayed, and I can click [Report Issue to Jazz Developers] to report the error: -
123649-startup.jpg

So I published a new build to make the new [Report Issue to Jazz Developers] feature available. However when I repeat the test with the published version, the dialog is different. The error is caught and reported with this form
123729-startuppublished.jpg
Clicking [Details] shows the same call stack as before, although as expected it does not include line numbers, but this is not my revised Startup form with the [Report Issue to Jazz Developers] button.

Is there a project option that I should change to ensure that Startup.vb will catch the error in the published version as it does in test? (This is the first time in 15 years of working with VB.NET that I get different results with the published version)

The project is written in VB.NET with .NET Framework 4.7.2. It is published as a ClickOnce application.

Developer technologies | .NET | Other
Developer technologies | VB
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Robert Barnes 101 Reputation points
    2021-08-16T22:43:09.33+00:00

    Sorry, there was an error with my post. The code at the top of the notes should have been formatted like this Private Sub Startup_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Try ... Dim Workbench As New JazzWorkbench JazzWorkbench = Workbench Workbench.AutoOpen = My.Settings.AutoOpen Workbench.ShowDialog() CloseJazzLog() 'Does nothing unless open Me.Close() Catch ex As Exception lblError.Text = ex.Message WriteJazzLog("Error:" & ex.Message) ... CloseJazzLog() End Try End Sub

    0 comments No comments

  2. Duane Arnold 3,216 Reputation points
    2021-08-17T03:24:24.123+00:00

    You could remove all try/catches out of the program. That will make all exception thrown unhandled exceptions that will be caught by the centralized exception handler.

    https://www.codeproject.com/Articles/43182/Centralised-Exception-Handling-in-C-Windows-Applic

    I had this working for a VB Windows form program. I even use it in Windows c# .net Core program.

    The key for you is to get the code into the VB Window from executed on Start up

    https://docs.devexpress.com/WindowsForms/119891/build-an-application/how-to-perform-actions-on-application-startup

    The code works the same way in VB as it does in c#. And if the exception is thrown in a DLL and it's unhandled reference by the Windows form project, the exception will be caught by the central handler.

    HTH

    0 comments No comments

  3. Robert Barnes 101 Reputation points
    2021-08-17T05:07:18.153+00:00

    Duane, thank you for answering. However,

    1. It is impractical to remove other Try/Catch code - JazzWorkbench is a large and complex program, and there are several places where specific Try/Catch blocks are needed to protect against particular error situations. However there are none in the active path from Startup to btnTest_Click that could have stopped Startup from catching the error
    2. In this particular case the general try/catch is being correctly executed anyway.
    3. The code is already executed on startup.
    4. Even if none of 1-3 were true, this would not explain why I get different results in debugging mode and published version of the same code. Run from Visual Studio and everything works perfectly, with the test error being caught in Startup. Publish the program and run the published version and the error does not appear to be trapped, although I can prove that Startup is entered.

    DETAIL

    My project names Startup as its startup form, so the first bit of my code that is executed is this Sub: -

    Public Class Startup  
        Property JazzWorkbench As JazzWorkbench  
        Property callstack() As Collection ' Of string  
        Property JazzLog As StreamWriter   '15.5:JazzLog  
        Dim JazzLogOpened As Boolean = False  
        Private Sub Startup_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load  
            Try  
                ...    
                Dim Workbench As New JazzWorkbench  
                JazzWorkbench = Workbench  
                Workbench.AutoOpen = My.Settings.AutoOpen  
                Workbench.ShowDialog()  
                CloseJazzLog() 'Does nothing unless open  
                Me.Close()  
            Catch ex As Exception  
                lblError.Text = ex.Message  
                ...  
    

    Workbench.ShowDialog() opens the Jazz Workbench window, which in the current test looks like this
    123797-in1r.jpg

    If the button [Test] (bottom right) is clicked, this code is executed: -

        Private Sub btnTest_Click(sender As System.Object, e As System.EventArgs) Handles btnTest.Click  
            '   Force error  
            Dim Field As Data2 = Nothing  
            If Field.CobolName = "Fred" Then  
                Field = Nothing 'nonsense stmt: program will have already failed  
            End If  
        End Sub  
    

    This immediately fails on the IF statement. As Field is currently nothing, a reference to the Data2 property CobolName causes an immediate failure. When I run this with Visual Studio, with a break point set on the statement
    Dim Field As Data2 = Nothing
    and I then step through the code, execution reaches
    If Field.CobolName = "Fred" Then
    Control is thrown to
    Catch ex As Exception
    in form Startup.vb. This is exactly what I want, and results in the Startup form being displayed, like this
    123768-startup.jpg

    If I click [Show Call Stack] then the display shows
    Filename: C:\Users\Robertbw10\source\repos\Jazz\Jazz\JazzWorkbench\JazzWorkbench.vb, Line:1370, Method:btnTest_Click
    [External Code] OnClick
    [External Code] OnClick
    [External Code] OnMouseUp
    [External Code] WmMouseUp
    [External Code] WndProc
    [External Code] WndProc
    [External Code] WndProc
    [External Code] DebuggableCallback
    [External Code] DispatchMessageW
    [External Code] System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
    [External Code] RunMessageLoopInner
    [External Code] RunMessageLoop
    [External Code] ShowDialog
    Filename: C:\Users\Robertbw10\source\repos\Jazz\Jazz\JazzWorkbench\Startup.vb, Line:22, Method:Startup_Load

    The top line
    Filename: C:\Users\Robertbw10\source\repos\Jazz\Jazz\JazzWorkbench\JazzWorkbench.vb, Line:1370, Method:btnTest_Click
    is the IF statement
    If Field.CobolName = "Fred" Then
    The last line is
    Filename: C:\Users\Robertbw10\source\repos\Jazz\Jazz\JazzWorkbench\Startup.vb, Line:22, Method:Startup_Load
    which is
    Workbench.ShowDialog()
    There is no user code anywhere between these that could have issued an alternative Try/Catch/End Try.

    0 comments No comments

  4. Robert Barnes 101 Reputation points
    2021-08-17T05:28:41.197+00:00

    Duane, I'll investigate Visual Basic Method #2 from the 2nd reference, this looks as if it might fix the problem. Although (as per my post of a few minutes ago) it doesn't explain why it isn't already working in the published version.

    Thanks for pointing me at this reference. I'll let you know (tomorrow probably) if I need further help, or if it has worked.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.