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

x
?
Solved

Exception handling - VB.net

Posted on 2012-08-12
6
Medium Priority
?
646 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?
0
Comment
Question by:koossa
  • 3
  • 2
6 Comments
 
LVL 30

Expert Comment

by:IanTh
ID: 38285126
I cannot see if your using system
see

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

Author Comment

by:koossa
ID: 38285129
Yes, I do?
0
 
LVL 13

Accepted Solution

by:
Pryrates earned 2000 total points
ID: 38285131
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.
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.

 

Author Comment

by:koossa
ID: 38285137
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?
0
 
LVL 13

Assisted Solution

by:Pryrates
Pryrates earned 2000 total points
ID: 38285143
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 ;)
0
 
LVL 13

Assisted Solution

by:Pryrates
Pryrates earned 2000 total points
ID: 38285155
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

0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question