[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

Getting a global exception handler to work

I have a still open question elsewhere, which involves this program
(see question # Q_21188378),
but part of that problem involves an unhandled exception.

I have the following very simple windows form program (the form only has a single button to exit and nothing else).

The program has a "dim oracleCommand..." statement.  I create a release installation package for the program, and I install it on two different PCs.  One is the PC on which the .net application was developed, and it is running Windows XP Pro.  The other PC is running W2K.

When I execute the application on the WinXP system, no problem.  But when I try on the W2K machine, I get an unhandled exception in a generic error box.  (By the way, if I create a version of the application in which the Dim Oraclecommand.. statement is commented out, it runs fine on both systems... so that statement is clearly the problem.)

I have been trying to include an exception handler to pick up this exception and provide more details.  I included the ..main and sub handler parts of the code below based upon other articles I have seen.  But I still don't see any msg box when the exception happens.

What idiotic thing am I doing wrong that is preventing the exception handler from working?  (And if you know why the Dim Oraclecommand statement may be causing the exception, please check out my other question and let me know.)

Here is the complete and total code, verbatim:  (You should be able to cut and paste this into your IDE)..
*************************************************
Imports System.Threading
Imports System.Data.OracleClient.OracleCommand
Imports Oracle.DataAccess.Client
Module Module1
'''''' the following is copied from
'''''''http://www.dotnet2themax.com/ShowContent.aspx?ID=8983e102-2ff7-4ac1-9620-386b166a91d8
''''''' as suggested by farsight
    Sub Main()
        ' setup the global error handler
        AddHandler Application.ThreadException, AddressOf UnhandledExceptionProc
        ' display the main form
        Application.Run(New Form1)
    End Sub

    ' the critical error handler
    Public Sub UnhandledExceptionProc(ByVal sender As Object, ByVal e As ThreadExceptionEventArgs)
        Try
            ' let the user decide what to do
            Dim msg As String = String.Format("A {0} exception has occurred.{2}{2}Additional " _
               & "information: {1}{2}{2}Click Yes to save current data{2}Click No to exit wit" _
               & "hout saving{2}Click Cancel to ignore error", e.Exception.GetType.Name, _
               e.Exception.Message, ControlChars.CrLf)
            Dim res As DialogResult = MessageBox.Show(msg, "Unhandled error", _
               MessageBoxButtons.YesNoCancel, MessageBoxIcon.Error)
            Select Case res
                Case DialogResult.Cancel
                    ' return to the main application
                    Exit Sub
                Case DialogResult.No
                    ' do nothing (the application will be exited)
'''''''''  I didn't include a "yes" response handling since I really don't care.. I just would like this
'''''''''  message box to display when it hits the exception
            End Select
        Catch ex As Exception
            ' a very serious error must have occurred, exit
        End Try
        ' If we get here, let's exit the application
        Application.Exit()
    End Sub
End Module
'''''''''''''''''''''''''''''''''''''
Imports System.Data.OracleClient.OracleCommand
Imports System.Threading
Imports Oracle.DataAccess.Client

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim cmdOracleCommand As New Oracle.DataAccess.Client.OracleCommand
#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(120, 64)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(60, 52)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "EXIT"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Test Deploy"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim idummy1 As Integer
        idummy1 = 1
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub
End Class
0
jrcooperjr
Asked:
jrcooperjr
  • 4
  • 3
1 Solution
 
Ignacio Soler GarciaCommented:
Why don't you try this to catch the exception? You don't need the added module I think ...

Imports System.Data.OracleClient.OracleCommand
Imports System.Threading
Imports Oracle.DataAccess.Client

Public Class Form1
    Inherits System.Windows.Forms.Form
   
    Private cmdOracleCommand As Oracle.DataAccess.Client.OracleCommand

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call
        Init()
    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(120, 64)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(60, 52)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "EXIT"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Test Deploy"
        Me.ResumeLayout(False)
    End Sub
#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim idummy1 As Integer
        idummy1 = 1
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub

    Private Sub Init()
        Try
            cmdOracleCommand = New Oracle.DataAccess.Client.OracleCommand
        Catch ex As Exception
            MsgBox(ex.Message()
        End Try
    End Sub
End Class
0
 
jrcooperjrAuthor Commented:
Hi... thanks for your input... but I used your code and it made no difference... I still got the unhandled exception..
I could even comment-out the "cmdOracleCommand..." statement in your "try" section, and still would get the exception.
The culprit (using your example) is the "Private cmdOracleCommand as..."    Whatever it is that VB.Net tries to do when it encounters this statement, that is when the exception is being thrown... so the exception handler in even the initially called sub has not been reached.  Still hoping for help with this (and with Q_21188378)..
thanks again..
0
 
Ignacio Soler GarciaCommented:
What is the exact text of the error message? I think that a variable declaration can't throw an exception because nothing is done, the variable is not init, is just declared ...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
jrcooperjrAuthor Commented:
Hi.... please check out the information I added at the bottom of Q_21188378
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21188378.html
The error message I am getting is just the generic "Application has generated an exception that could not be handled" and then gives the process ID and the Thread ID.  The statement that is causing the exception (in the example I gave in the other question) is "Dim cmdOracleCommand As New Oracle.DataAccess.Client.OracleCommand".  I also didn't think that a variable declaration itself would be able to throw an exception, but this one must be doing something... if this one line is commented out, there is no exception.
Thanks for your help...


0
 
Ignacio Soler GarciaCommented:
Hello again ...

Dim cmdOracleCommand As New Oracle.DataAccess.Client.OracleCommand (your code)

is not the same as

Dim cmdOracleCommand As Oracle.DataAccess.Client.OracleCommand

cmdOracleCommand = New Oracle.DataAccess.Client.OracleCommand (my code)

Your code executes the New method and there an exception can be thrown, of course. My code only declares a variable and the new method is executed inside a try catch block. It isn't the same at all. So, taking this in mind, you get the exception on the first line or on the second one?

SoMoS
0
 
Ignacio Soler GarciaCommented:
Did you checked my last comment jrcooperjr?

SoMoS
0
 
jrcooperjrAuthor Commented:
Hello SoMoS (and others)...
Again, thanks for your input...
It finally dawned on me what you were trying to get me to accomplish (i.e., putting the "new" method itself inside the try..catch structure) and once I did that, I was able to get information about the exception...

which in turn gave me the information I needed to understand what the problem was in environment of the second machine.

(for those interested in what I found about Oracle components that one needs to run .net applications that aren't included in .net deployment packages, see my other question at

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21188378.html

)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now