We help IT Professionals succeed at work.

Exception handling - VB.net

Medium Priority
683 Views
Last Modified: 2012-08-12
I'm trying to figure out how to improve my exception handling in VB.net.

Private Sub cmdTest_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTest.Click
  1: Try
  2:   Code......
  3:   ...............
  4:   Call MyTestProc
  5:   ................
  6:   ..................
  7: Catch ex As Exception
  8:   Call MyReportError(ex, "frmMain", "cmdTest_Click")
End Sub

Privat sub MyTestProc()
   9: Try
  10:   Code......
  11:   ...............
  12:   ..............
  13:   ................
  14:   ..................
  15: Catch ex As Exception
  16:   Call MyReportError(ex, "frmMain", "MyTestProc")
End Sub

  Friend Sub MyReportError(ByVal ex As Exception, ByVal sClass As String, ByVal sProc As String)
    '''HERE I JUST WRITE A LOG FILE, WITH ALL THE ERROR DESCRIPTION, ERR NUMBER, LINE NUMBER, ETC AND SEND IT TO ME IF THE USER WANTS TO REPORT THE ERROR.
  End Sub

Open in new window


If there is an error on line 13 for example then the error report log file says, error on line 4.
My question is how would I get the real error line (line 13) in my log file and possible the Call stack where it originates from?
Comment
Watch Question

Commented:
I cannot see if your using system
see

http://www.startvbdotnet.com/language/

Author

Commented:
Yes, I do?
Commented:
you can simply write
ex.ToString() 

Open in new window

in your logger method.
That will give you the real codeline that raised the error.

Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
            DoSomeStuff(0)
        Catch ex As Exception
            Call MyReportError(ex, "frmMain", "Button1_Click")
        End Try

    End Sub

    Private Sub DoSomeStuff(ByVal no As Integer)
        Try
            Dim ff As Integer = 10
            Dim result As Integer
            result = ff / no
        Catch ex As Exception
            Call MyReportError(ex, "frmMain", "DoSomeStuff")
        End Try

    End Sub

    Friend Sub MyReportError(ByVal ex As Exception, ByVal sClass As String, ByVal sProc As String)
        'HERE I JUST WRITE A LOG FILE, WITH ALL THE ERROR DESCRIPTION, ERR NUMBER, LINE NUMBER, ETC AND SEND IT TO ME IF THE USER WANTS TO REPORT THE ERROR.

        Dim sr As New System.IO.StreamWriter("log.txt", True)
        sr.WriteLine("----------------------------------------")
        sr.WriteLine(sClass)
        sr.WriteLine(sProc)
        sr.WriteLine(ex.ToString())
        sr.Close()
    End Sub
End Class

Open in new window


This will give me a log entry saying there is an arithmetic error in line 15.

Of course it will only work, if you have the pdb-file along with your application.
Without it, there is no code line provided at all.

Author

Commented:
I'm using Err.Erl to get the line numbers and it return error on line 4.

And how would I get the call stack, will it be in the ex.ToString?

Is there any risk of distributing the .pdb file with my application? Could they maybe decompile it or get some valuable passwords or any other import information from that file?
Commented:
if you do not obfuscate your source by a special tool, there is no need to use a pdb to decompile your program. So I do not see there any increased security issues by deploying it.

You can store the pdb as a resource file within your application, but I have never done it that way. There you might need to do a little research.

No, ex.ToString() is a two-liner.
Exception type, description, Method, codeline

There is no call stack.
Apart from using ex.ToString(), you can still write some extra info in your debug.

I will have a look into call stack, maybe we can figure something out ;)
Commented:
you can try something like that:
    Friend Sub MyReportError(ByVal ex As Exception, ByVal sClass As String, ByVal sProc As String)
        'HERE I JUST WRITE A LOG FILE, WITH ALL THE ERROR DESCRIPTION, ERR NUMBER, LINE NUMBER, ETC AND SEND IT TO ME IF THE USER WANTS TO REPORT THE ERROR.

        Dim sr As New System.IO.StreamWriter("log.txt", True)
        sr.WriteLine("----------------------------------------")
        sr.WriteLine(sClass)
        sr.WriteLine(sProc)
        sr.WriteLine(ex.ToString())

        sr.WriteLine("--------------- TRACE -------------------")
        Dim trace As New System.Diagnostics.StackTrace(True)
        sr.WriteLine(trace.ToString())
        sr.WriteLine("----------------------------------------")

        sr.Close()
    End Sub

Open in new window


This will give you the call stack, but you will still need ex.ToString() to get the line, where the actual error occured:

----------------------------------------
frmMain
DoSomeStuff
System.OverflowException: Die arithmetische Operation hat einen Überlauf verursacht.
   bei ErrorTest.Form1.DoSomeStuff(Int32 no) in C:\ErrorTest\ErrorTest\ErrorTest\Form1.vb:Zeile 15.
--------------- TRACE -------------------
   bei ErrorTest.Form1.MyReportError(Exception ex, String sClass, String sProc) in C:\ErrorTest\ErrorTest\ErrorTest\Form1.vb:Zeile 33.
   bei ErrorTest.Form1.DoSomeStuff(Int32 no) in C:\ErrorTest\ErrorTest\ErrorTest\Form1.vb:Zeile 17.
   bei ErrorTest.Form1.Button1_Click(Object sender, EventArgs e) in C:\ErrorTest\ErrorTest\ErrorTest\Form1.vb:Zeile 4.
   bei System.Windows.Forms.Control.OnClick(EventArgs e)
   bei System.Windows.Forms.Button.OnClick(EventArgs e)
   bei System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   bei System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   bei System.Windows.Forms.Control.WndProc(Message& m)
   bei System.Windows.Forms.ButtonBase.WndProc(Message& m)
   bei System.Windows.Forms.Button.WndProc(Message& m)
   bei System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   bei System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   bei System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   bei System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   bei System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   bei System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   bei System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
   bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
   bei Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
   bei ErrorTest.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:Zeile 82.

----------------------------------------

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.