tracing  - Good practice?

Posted on 2012-08-30
Last Modified: 2012-08-31
I have never used tracing.

I have always used a simple technique going back to vb6 and vba like this

Dim LN as double = 0


 Ln = 1.01
            CnnLodge.ConnectionString = strCnnStoresData

            Ln = 1.02
           Ln = 1.021
            cmd.CommandType = Data.CommandType.StoredProcedure
            Ln = 1.022
            cmd.Parameters.Add("@JDEBatchNo", SqlDbType.Int)
            Ln = 1.023
            cmd.Parameters("@JDEBatchNo").Value = JDEBatchNo

Ln = 1.024
            DR = cmd.ExecuteReader()

Catch ex As Exception
            Errs.LogError(ex, LogFile, "chqImportLodgements Ln:" & Ln)

End Try

so in my error log I see the error and the ln number so I know where the problem occured.

Is this a good method or is it very poor programming. It does not noteable effect the program but would it be considered good practice.
Question by:Eamon
    LVL 83

    Assisted Solution

    I have never seen that. But I use a similar approach where I enclose important code block, where an exception can be thrown, and then I log with information of that block so I know which area I need to look into and the rest of the information can be gather from exception itself.
    LVL 16

    Assisted Solution

    Poor programming.

    First of all, it is a waste of processor time attempting to constantly update a variable just to track where you are.
    Second, you're using a double rather than an integer, so it takes longer to execute this "extra" code.
    Third, modern optimizing compilers might change your code such that your position tracking variable changes might not correspond to the other lines of code around it.  In other words, a modern compiler will modify your code and execute it in a different sequence when the change in sequence doesn't affect the final results of the execution of the subroutine and will execute faster.

    If you MUST know what line number an error has occured, several languages (VB6 and VB.Net specifically) allow you to simply include line numbers in the source code.  In the case of VB, the ERR object tracks the line number for you when an error occurs:

     10: On Error Goto 999
     20: CnnLodge.ConnectionString = strCnnStoresData
     30: CnnLodge.Open()
     40: cmd.CommandType = Data.CommandType.StoredProcedure
     50: cmd.Parameters.Add("@JDEBatchNo", SqlDbType.Int)
     60: cmd.Parameters("@JDEBatchNo").Value = JDEBatchNo
     70: DR = cmd.ExecuteReader()
    120: Exit Sub
    999: Errs.LogError(Err.Number, LogFile, "chqImportLodgements Ln:" & Err.Erl)

    Open in new window

    But almost no one ever programs like this any more.  The better approach is to simply keep your subroutines small and have your error logging include what subroutine it was in when the error occured.  That way, the combination of the error number and the name of the subroutine should help you determine where the error occured.

    The other thing I see wrong with the error handler is you've simply recorded the error.  But I don't see where you've included any logic for "what to do next".  You've caught the error, but you don't seem to be doing anything to recover from it.  Will your program continue to execute if it jumps to the error handler?  I would expect to see where the code was returned to some sort of a stable state (which depending upon the situation, perhaps you didn't need to do anything to "clean up" after the error).
    LVL 1

    Author Comment

    image of code
    How much processor time am I wasting? Should it cause preformance issues.

    I am using VS2010 and not worried about vb. Are you telling me that the .net compiler won;t execute the code in the order I write it. I have used this method for a long time and it always pin point s where an error occured.

    It is hard to keep routines small if you are filling a table that has 30 - 40 columns in it then you will have to do them in the same routine. If your error come back with invalid use of null or similar how are you supposed to know where the error happened.

    When in design the error does actually tell me the line no of the code but not when I deploy the program.
    LVL 83

    Assisted Solution

    >If your error come back with invalid use of null or similar how are you supposed to know where the error happened.

    Stack Trace usually gives a good idea of where the problem is. In database related logic, it would be either error in connecting to server, error in executing command, or other errors if you have more logic to manipulate data but stack trace gives a good idea of what and where the error is.
    LVL 1

    Author Comment

    can I conclude that what I am doing then is

    1 inefficient
    2 bad practice
    3 not needed due to Stack Trace.
    LVL 83

    Expert Comment

    My conclusion would be that there are other preferrable options.
    LVL 40

    Accepted Solution

    If you need is simply to know the line that trigger an error, let the system works for you.

    Simply generate a pdb file by setting "Generate debug info" in the "Advanced Compile Options" of the Compile tab of the project's Properties window. Be sure to distribute the generated .pdb file along with your application.

    With the .pdb, whenever you get an error, record ex.ToString (if you use the default ex name for your exception variables) somewhere (database, file, e-mail). It will give you all the information you need, such as in the following. It displays both what happens in your code an in the framework. No that there is a reference to the source code and the line number on lines that come from your own code. Simply load the file an activate Ctrl-G to go to the specified line. In order to get to the proper line, be sure to have a copy of the version of the source code that was used for the version in use.

    System.IO.FileNotFoundException: Could not find file "H:\Prog\ PhotoCat\Toto.mdf".
    File name: "H:\Prog\Projects\PhotoCat\Data\Toto.mdf"
    	at PhotoCat.FormTests.btnTest_Click(Object sender, EventArgs e) in H:\Prog\Projects\PhotoCat\FormTests.vb:line 209
    	at System.Windows.Forms.Control.OnClick(EventArgs e)
    	at System.Windows.Forms.Button.OnClick(EventArgs e)
    	at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
    	at System.Windows.Forms.Application.RunDialog(Form form)
    	at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
    	at System.Windows.Forms.Form.ShowDialog()
    	at PhotoCat.FormPhotoCat.mnuTests_Click(Object sender, EventArgs e) in H:\Prog\Projects\PhotoCat\FormPhotoCat.vb:line 1615
    	at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
    	at System.Windows.Forms.MenuItemData.Execute()
    	at System.Windows.Forms.Command.Invoke()
    	at System.Windows.Forms.Application.RunDialog(Form form)
    	at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
    	at System.Windows.Forms.Form.ShowDialog()
    	at PhotoCat.modPhotoCat.Main() in H:\Prog\Projects\PhotoCat\PhotoCat.vb:line 72

    Open in new window

    LVL 1

    Author Closing Comment

    Thank You

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
    Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    730 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now