Does any one know how to add line numbers to your code for error handling
I want to use Information.erl in my error routines so I know the exact line the error happend. In vb6 I had an addin that would add line numbers for me.
Is there anything simillar for vbExpress
Note: This is different from adding line numbers by going to Tools > Options > Editor Options.
Thanks

Add line numbers to code
Randy Shoopman
I had a look and was n not abale to find it.
Any ideas on how it's done
I tried using
Try Dim i As Integeri = 1 / 2
i = i / 0
Catch ex As System.ExceptionMsgBox(ex.Message)
MsgBox(Erl())
End TryBut it does not return the line number
sun21170
First of all, the legacy VB6 line numbers (e.g., 10: 20: , etc.) and the numbers you see by using Tools > Options > Editor Options are two different sets of numbers.
The old fashioned, legacy VB6 numbers are part of your code.
The numbers displayed by the Editor Options are just IDE numbers for the developer to keep track of where certain lines of code are and for reference purposes during code reviews and internal documentation. They are only used by the IDE and are not part of your code or your program.
You can use the old 10: 20: etc type. numbers in your code for debugging purposes, which I will explain in a moment. I would say that the only places to put these numbers would be where you want to display the line number and, in some VERY rare cases, where you have a long segment of procedural code and want to use GoTo to exit out of it (in fact, intellisense will display all the available 10: type line numbers available when you use GoTo).
Now, how to use those numbers. Some background:
On Error is a construct of legacy VB languages. The VB.NET dialect of the VB.NET/C# language has On Error in its instruction set to support people coming to VB.NET from other languages such as Fox Pro and VB6. It also has an Error object to support the old raise error constructs from legacy languages. You should stop using VB6 code in your programs and start using VB.NET code, instead. Do you still want to be coding in Visual Basic 3 years from now when everyone else is using VB.NET/C#
The VB.NET/C# language uses a new construct called Try . . . Catch blocks. It's part of the structured exception handling in VB.NET/C# and all .NET languages (even COBOL). Using structured exception handling allow all your .NET programs to pass and handle each other's errors. So if your VB.NET program calls a COBOL.Net program which, in turn, invokes an object written in C#, if any of the modules and components in the chain has an error, they can handled consistently. Your VB.NET program can "handle" any errors raised by the COBOL or C# code.
You should be using Try . . . Catch, not On Error or Err.Raise(). If you want to raise an error, use "Throw", which is consistent with the structured exception handling we enjoy with .NET managed code.
So where do the line numbers come in
Here is some code:
Private Sub btnPressMe_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnPressMe.Click
10:
End Sub
Private Function Sum(ByVal int1 As Integer, ByVal int2 As Integer) As Integer
40: ' Some Code here
50:
End Function
The approach I took, above, was to sparingly use coded line numbers in places where I needed to know the line number for debugging -- lines 10: 40: and 50: in this case.
Rather than use Err.Raise( ), I used Throw. Rather than using VB6 code, MsgBox( ), I used VB.NET code, MessageBox.Show. If you're coding in VB.NET, you should use that language and avoid backward compatible commands from other languages.
Whenever I wanted to display a line number in my error message, I just added a new number (e.g., 40:) and put that number in the message. Yes, it's a workaround and maybe others can come up with other approaches, but this approach could solve your program.
I think you'll find you'll have less problems like this in the future if you consistently use VB.NET commands in your code and stop using pieces of other languages (such as VB6), even if you are more comfortable with using legacy code. If you are using a new language, you will find that, if you use only that language in your code, you will become comfortable with the new language pretty fast.
Good luck, and I hope this helps you.
- - Jerry Bucknoff
Samant B Jain
The Exception class should tell you exactly the line number where an exception occured.
Su4v3
Try
Catch ex As Exception
MessageBox.Show(ex.ToString())
MessageBox.Show(ex.StackTrace)
End Try
When in the code editor, the current line number is displayed on the status bar. You can display line numbers in the code window by editing the IDE options. In VB 2005 Express it is "Text Editor -> General -> Basic -> Line numbers". You will have to check the "Show all settings" box for this option to be visible.
Namfon
Thanks for the info. I have a better understanding of .net error handling now.
I guess you cannot add line numbers to a whole routine at once then. Just for the ease of debugging as it pinpoints the line number.
I have been moving to full .net and getting rid of all the vb6 code. Is there a way to turn off vb6 code so it will throw an error