How to use stacktrace to return error line number in vb.net

I am trying to create some kind of error detection method that will return the error line number. We have an interrupted email that is sent when the process is interrupted, which gives us err.number and err.description, but I would like to know where the errors really are.

I know you can do the following:

1: code here 2: code here 3: code here 

etc .. and use ERL to get the number, but it would be tedious to print each line.

Is there a way to do this automatically, or would it be easier to use Stacktrace? If Stacktrace is better, could you show me an example?

+8
source share
6 answers

Generating line numbers in the exception stack trace is a built-in function for the CLR. However, you need to provide the necessary information to match the code with the line number. Switch to the Release configuration of your project. Project + Properties, Compile tab, Advanced Compile Options. Change the Generate Debug Information option from pdb-only to Full. Deploy the .pdb files with your program.

Beware that the line number you get is always an estimate, so don't blindly believe what you see. The display is imperfect due to the use of flash optimizer optimization methods and, otherwise, code movement to speed up the program.

+8
source

I adapted the example from another forum, in my case I did not get the line number where the error was caused, so I started playing and found a solution, the code looks like this:

 Public Class Form1 Private Sub a2() Dim b As Integer = 0 Dim a As Integer = 1 / b End Sub Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Try a2() Catch ex As Exception Dim st As New StackTrace(True) st = New StackTrace(ex, True) MessageBox.Show("Line: " & st.GetFrame(0).GetFileLineNumber().ToString, "Error") End Try End Sub End Class 

In this example, line 4 will throw an error exception, but as soon as I applied the principle in a real application, the line was 0, so I started playing with the index in the GetFrame property, it ranges from 0 to 4, when I put 4 in the object, EUREKA, I got the line number causing the problem.

+14
source

You should definitely use stack tracing, since you can use the global catch catch mechanism, which you only need to encode once.

To get the exact line that the error was thrown onto, you will need to send the pdb files with your application. These pdb files contain debugging information, including the error line number.

If you want to know how to catch unhandled exceptions correctly, check out this code article .

0
source
  Try Dim x As Integer x = " " Catch ex As Exception Dim trace = New Diagnostics.StackTrace(ex, True) Dim line As String = Strings.Right(trace.ToString, 5) Dim nombreMetodo As String = "" For Each sf As StackFrame In trace.GetFrames nombreMetodo = sf.GetMethod().Name & vbCrLf Next MessageBox.Show("Error en Linea number: " & line & vbCrLf & ex.Message & vbCrLf & "Metodos : " & nombreMetodo) End Try 
0
source
  Try Dim x As Integer x = " " Catch ex As Exception Dim trace = New Diagnostics.StackTrace(ex, True) Dim line As String = Strings.Right(trace.ToString, 5) Dim nombreMetodo As String = "" Dim Xcont As Integer = 0 For Each sf As StackFrame In trace.GetFrames Xcont = Xcont + 1 nombreMetodo = nombreMetodo & Xcont & "- " & sf.GetMethod().ReflectedType.ToString & " " & sf.GetMethod().Name & vbCrLf Next MessageBox.Show("Error en Linea number: " & line & ex.Message & vbCrLf & "Metodos : " & vbCrLf & nombreMetodo) End Try 
0
source

You can use StackTrace to get the line number on error.

 Try 'Put your code here Catch ex As Exception Dim trace = New Diagnostics.StackTrace(ex, True) Dim line As String = Right(trace.ToString, 5) MessageBox.Show("'" & ex.Message & "'" & " Error in- Line number: " & line) End Try 
-one
source

All Articles