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.
Mauricio
source share