Hi @test code ,
To capture the exact line of code that caused the error, you can leverage the .StackTrace
property of the exception object.
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Try
Dim d As Integer
Dim c As Integer = d / 0
MsgBox(c.ToString)
Catch ex As Exception
' Extract stack trace information
Dim stackTrace As New StackTrace(ex, True)
Dim frame As StackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1)
Dim fileName As String = frame.GetFileName()
Dim lineNumber As Integer = frame.GetFileLineNumber()
Dim methodName As String = frame.GetMethod().Name
Dim errorDetails As String = $"Error Message: {ex.Message}" & vbCrLf &
$"File: {fileName}" & vbCrLf &
$"Method: {methodName}" & vbCrLf &
$"Line Number: {lineNumber}" & vbCrLf &
$"Stack Trace: {ex.StackTrace}"
MsgBox(errorDetails)
End Try
End Sub
End Class
Best Regards.
Jiachen Li
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.