Displaying the procedure or sub where a error occurs to a user.

Is there a way to display the name of the routine where an error occurs in my code using the framework Obviously, I could put the name of each routine in a message and so forth but that is just a bad design. I was hoping that I would be able to use Reflection or Exception handling to do this in a structured way.

I have several applications that are monitored by IT professionals internal to the company who need as much information as possible about an error that occurs when support is not immediately available. Also there are reporting considerations for tracking the errors.



Answer this question

Displaying the procedure or sub where a error occurs to a user.

  • Carlos Hdez

    The following example may help.

    Create a console application and paste this code in.

    Module Module1

    Sub Main()
    Foo.xyz()
    End Sub

    End Module

    Public Module Foo
    Public Sub xyz()
    Try
    Throw New Exception("ffff")

    Catch ex As Exception
    MsgBox(ex.TargetSite.ReflectedType.FullName.ToString & " Function:" & ex.TargetSite.Name.ToString)
    End Try
    End Sub
    End Module


  • Chrisql

    I alway do my messageboxes like this:

    Try

    code......

    catch e as exception

    msgbox("Exception" & e.message,MsgBoxStyle.Information,"ReadRow - I/O Error")

    End Try....

    Where the routine name is ReadRow



  • Pat Backowski

    Also check out the Application.ThreadException event.

    and Exception.StackTrace



  • jornjorn

    (source code with test app  here)

    I have this component I put on my main forms to trap all unhandled errors using the Application.ThreadException event-

    1.1 code but it compiles in 2.0

    create a new class library project call it ErrorTrap.

    add references to System Windows.Forms and System.Drawing

    add a new form called error form put this code in it (again this is 1.1 code - download the source and convert for 2.0)

    Imports System.Windows.Forms

    Public Class ErrorForm
        Inherits System.Windows.Forms.Form

        #
    Region " Windows Form Designer generated code "
        

        Public
    Sub New()
            MyBase.New()
            InitializeComponent()
        End Sub
        
        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
        
        
    Private components As System.ComponentModel.IContainer
        

        
    Friend WithEvents details As System.Windows.Forms.TextBox
        
    Friend WithEvents message As System.Windows.Forms.TextBox
        <System.Diagnostics.DebuggerStepThrough()> _ 
        
    Private Sub InitializeComponent()
           
    Dim resources As System.Resources.ResourceManager = _ 
               
    New System.Resources.ResourceManager(GetType(ErrorForm))
           
    Me.Button1 = New System.Windows.Forms.Button
            Me.Label1 = New System.Windows.Forms.Label
           
    Me.GroupBox1 = New System.Windows.Forms.GroupBox
           
    Me.details = New System.Windows.Forms.TextBox
           
    Me.message = New System.Windows.Forms.TextBox
           
    CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).BeginInit()
           
    Me.GroupBox1.SuspendLayout()
           
    Me.SuspendLayout()
           
    '
           
    'Button1
           
    '
           
    Me.Button1.DialogResult = System.Windows.Forms.DialogResult.Cancel
           
    Me.Button1.Location = New System.Drawing.Point(376, 312)
           
    Me.Button1.Name = "Button1"
           
    Me.Button1.Size = New System.Drawing.Size(62, 27)
           
    Me.Button1.TabIndex = 0
           
    Me.Button1.Text = "&Ok"
           
    '
           
    'Label1
           
    '
           
    Me.Label1.AutoSize = True
           
    Me.Label1.Location = New System.Drawing.Point(12, 19)
           
    Me.Label1.Name = "Label1"
           
    Me.Label1.Size = New System.Drawing.Size(61, 16)
           
    Me.Label1.TabIndex = 2
           
    Me.Label1.Text = "Description"
           
    Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight
           
    '
           
    'GroupBox1
           
    '
           
    Me.GroupBox1.Controls.Add(Me.details)
           
    Me.GroupBox1.Location = New System.Drawing.Point(12, 55)
           
    Me.GroupBox1.Name = "GroupBox1"
           
    Me.GroupBox1.Size = New System.Drawing.Size(452, 247)
        
        Me.GroupBox1.TabIndex = 3
           
    Me.GroupBox1.TabStop = False
           
    Me.GroupBox1.Text = "Detailed Information"
           
    '
        
        'details
           

           
    Me.details.BackColor = System.Drawing.SystemColors.Control
           
    Me.details.Dock = System.Windows.Forms.DockStyle.Fill
        
        Me.details.Location = New System.Drawing.Point(3, 16)
           
    Me.details.Multiline = True
           
    Me.details.Name = "details"
           
    Me.details.ReadOnly = True
           
    Me.details.ScrollBars = System.Windows.Forms.ScrollBars.Both
           
    Me.details.Size = New System.Drawing.Size(446, 228)
           
    Me.details.TabIndex = 3
           
    Me.details.Text = ""
           
    Me.details.WordWrap = False
           
    '
           
    'message
           
    '
           
    Me.message.Location = New System.Drawing.Point(75, 16)
           
    Me.message.Name = "message"
           
    Me.message.ReadOnly = True
           
    Me.message.Size = New System.Drawing.Size(381, 20)
           
    Me.message.TabIndex = 2
           
    Me.message.Text = ""
           
    '
           
    'ErrorForm
           
    '
           
    Me.AcceptButton = Me.Button1
           
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
           
    Me.CancelButton = Me.Button1
           
    Me.ClientSize = New System.Drawing.Size(468, 352)
           
    Me.Controls.Add(Me.message)
           
    Me.Controls.Add(Me.GroupBox1)
           
    Me.Controls.Add(Me.Label1)
           
    Me.Controls.Add(Me.Button1)
           
    Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
            Me.MaximizeBox = False
           
    Me.MinimizeBox = False
           
    Me.Name = "ErrorForm"
           
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
           
    Me.Text = "Application Exception"
           
    CType(Me.EventLog1, System.ComponentModel.ISupportInitialize).EndInit()
           
    Me.GroupBox1.ResumeLayout(False)
           
    Me.ResumeLayout(False)
        
    End Sub
        
        
    Friend WithEvents Button1 As System.Windows.Forms.Button
        
    Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox
        
    Friend WithEvents Label1 As System.Windows.Forms.Label
        #
    End Region
        
        
    Private _exception As Exception
        
    Public Sub Display(ByVal ex As Exception)
            _exception = ex
           
    Me.ShowDialog()
        
    End Sub
        
        
    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
           
    Me.Text = String.Format("{0} - {1}", Me.Text, Application.ProductName)
            message.Text = _exception.Message
            details.Text =
    String.Format("{1} - {2} {0} at: {3}.{4}{0}Stack trace:{0}{5}", _
                System.Environment.NewLine, _
                _exception.GetType().Name, _exception.Message, _
                _exception.TargetSite.ReflectedType.Name(), _
                _exception.TargetSite.Name, _exception.StackTrace)
           
    MyBase.OnLoad(e)
        
    End Sub
    End
    Class

    Public Class ExceptionHandler
        
    Inherits System.ComponentModel.Component
        
        
    Private components As System.ComponentModel.IContainer
        
    Private _ThreadExceptionArgs As System.Threading.ThreadExceptionEventArgs
        
    Public Event ExceptionHandled As Threading.ThreadExceptionEventHandler
        
        
    Public Sub New(ByVal Container As System.ComponentModel.IContainer)
            
    MyBase.New()
            
    Container.Add(Me)
            If DesignMode Then Exit Sub
        
       
    AddHandler Application.ThreadException, AddressOf Me.ExceptionHandler
        End Sub

        <System.Diagnostics.DebuggerStepThrough()> _
        
    Private Sub InitializeComponent()
            components =
    New System.ComponentModel.Container
        
    End Sub
        
        
    Public Sub New()
           
    If DesignMode Then Exit Sub
           
    AddHandler Application.ThreadException, AddressOf Me.ExceptionHandler
        
    End Sub
        
        
    Private Sub ExceptionHandler(ByVal sender As Object, _ 
                   
    ByVal t As System.Threading.ThreadExceptionEventArgs)
            _ThreadExceptionArgs = t
           
    Dim ef As New ErrorForm
           
    Try
               
    ef.Display(t.Exception)
                
    Me.OnExceptionHandled()
            
    Catch ex As Exception
                MessageBox.Show("Fatal Error", "Fatal Error", MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
            
    Finally
                
    ef.Dispose()
            
    End Try
        
    End Sub

        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
           
    If Not DesignMode Then _
               
    RemoveHandler Application.ThreadException, _
                   
    AddressOf Me.ExceptionHandler
           
    If disposing Then
               
    If Not (components Is Nothing) Then
                   
    components.Dispose()
               
    End If
           
    End If
           
    MyBase.Dispose(disposing)
        
    End Sub
        
        
    Protected Overridable Sub OnExceptionHandled()
           
    RaiseEvent ExceptionHandled(Me, _ThreadExceptionArgs)
        
    End Sub
    End
    Class

    you can add ExceptionHandler to your toolbox and drop it on your main form to trap all unhandled errors. It displays the Message as well as detailed Exception information  - TargetSite and Stack trace.

    source code with test app  here



  • theprogrammer

    Is there a way to use the framework to return the routine name

    programmatically

    Try

    code.....

    catch e as exception

    msgbox("Exception: " & e.message, MsgBoxStyle.Information, Function())

    End Try...


  • Displaying the procedure or sub where a error occurs to a user.