Method to find the exact line of code that caused an error in VB.NET using Try Catch

test code 1 Reputation point
2024-12-23T06:41:17.56+00:00

When an error occurs while executing the project (in this case, dividing by zero), the message box displays the exception details, including the file path, procedure and line number where the error occurred, such as "Form1.vb:line 5".

Code:

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
            MsgBox(ex.toString)
        End Try
    End Sub
End Class

For the above code, Is there any code that can be placed in catch part so that message box should display the exact line of code that caused the error, i.e., "Dim c As Integer = d / 0" which would be helpful for logging specific line of code where error occurred for larger coding projects. I want to get the line of code along with line number details for easier Debugging.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,760 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 32,961 Reputation points Microsoft Vendor
    2024-12-23T07:17:53.9666667+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.