tracing  - Good practice?

Posted on 2012-08-30
Medium Priority
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

CodeCruiser earned 800 total points
ID: 38350018
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

HooKooDooKu earned 400 total points
ID: 38350090
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).

Author Comment

ID: 38350502
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.
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

LVL 83

Assisted Solution

CodeCruiser earned 800 total points
ID: 38351027
>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.

Author Comment

ID: 38351043
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

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

Accepted Solution

Jacques Bourgeois (James Burger) earned 800 total points
ID: 38353226
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


Author Closing Comment

ID: 38354217
Thank You

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.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses

601 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